0

I am taking input in the java.time.LocalDateTime format as I need hours/minutes.

input : 2020-04-19T01:03:50

To interact with oracle DB from jpaRepostory, I am using

import org.joda.time.DateTime;
List<String> findApps(@Param("lastModifiedDate") DateTime lastModifiedDate);

For conversion from LocalDate to dateTime I am using this way

DateTime.parse(startDate.toString(), DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"))

But I am getting this result and unable to fetch any records.

2020-04-19T01:03:50.000+05:30

Can anyone help me.

PS: Thank you in advance.

david
  • 79
  • 7
  • 1
    I think instead of using startDate.toString() you must somehow format startDate using another mask. – shikida Jun 26 '21 at 20:26
  • 1
    Why do you still use Joda when you already have access to java.time? Just remove Joda from the project and then you don't need to convert anything anymore. – Tom Jun 26 '21 at 20:45
  • Your result looks correct. Which result would you need to have in order to be able to fetch records? Asking not knowing whether or how you can use JPA Repository with Joda-Time. – Ole V.V. Jun 28 '21 at 04:43

2 Answers2

2

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

public class Main {
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.parse("2020-04-19T01:03:50");
        OffsetDateTime odtIndia = ldt.atZone(ZoneId.of("Asia/Kolkata")).toOffsetDateTime();
        System.out.println(odtIndia);
        OffsetDateTime odtUtc = odtIndia.withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odtUtc);
    }
}

Output:

2020-04-19T01:03:50+05:30
2020-04-18T19:33:50Z

ONLINE DEMO

The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Learn more about the modern Date-Time API from Trail: Date Time.

Using Joda API:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime ldt = new LocalDateTime("2020-04-19T01:03:50");
        DateTime dtIndia = ldt.toDateTime(DateTimeZone.forID("Asia/Kolkata"));
        System.out.println(dtIndia);
        DateTime dtUtc = dtIndia.toDateTime(DateTimeZone.UTC);
        System.out.println(dtUtc);
    }
}

Output:

2020-04-19T01:03:50.000+05:30
2020-04-18T19:33:50.000Z

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

You can try something like this

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Joda {

    public static void main(String[] args) {
        DateTime startDate = new DateTime();    
        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
        String formatted = dtf.print(startDate); //prints 2021-06-26T17:32:50 
        System.out.println(formatted);      
        DateTime parsed = DateTime.parse(formatted, DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"));
    }

}
shikida
  • 485
  • 2
  • 10