Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

migrate from Joda time library to Java time(Java 8)

I am trying to migrate from Joda time library to Java time (Java 8). I am unable to find equivalent of ISODateTimeFormat.dateOptionalTimeParser() in java.time

Joda ISO formatter has nice parsers:

ISODateTimeFormat.dateTimeParser() : generic - selects parser based on string parsed. Similarly: ISODateTimeFormat.dateOptionalTimeParser().

I am finding it difficult to change Joda time to java.time. Can some one guide me?

example:

String dateTimeString = "2015-01-01T12:29:22+00:00"; 
String dateTimeString2 = "2015-01-01T12:29:22";

When I parse this string using joda time then

ISODateTimeFormat.dateTimeParser().withZone("EST")

can handle both without as problem. Which is equivalent of this in java time?

Using java 8, ZonedDateTime with ISO_Zoned_date_time is not able to handle both.

like image 946
Shishir Avatar asked May 05 '15 20:05

Shishir


People also ask

What is the replacement of Joda-time Library in java 8?

Correct Option: D. In java 8,we are asked to migrate to java. time (JSR-310) which is a core part of the JDK which replaces joda library project.

Is Joda-time format followed in java 8?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

Which time format is followed in java 8?

For time patterns, use mm for two-digit minute and ss for two-digit second. The hh pattern generates the two-digit hour for a 12-hour clock (e.g., 6PM is "06") and HH generates the two-digit hour for a 24-hour clock (e.g., 6PM is "18").


1 Answers

You cannot use a predefined formatter but you can construct your own one (and assign it to a static constant) using following pattern:

static final DateTimeFormatter DATE_TIME_OPTIONAL_OFFSET =
    DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss[xxx]");

Attention: If you parse an input containing only date and time but without offset (and without any offset/zone-default) then the result can only be a LocalDateTime, not a global timestamp.

Please also note the different behaviour of method withZone(...).

Joda-Time

When parsing, this zone will be set on the parsed datetime.     
A null zone means of no-override. If both an override chronology
and an override zone are set, the override zone will take precedence
over the zone in the chronology.

Java-8 (JSR-310)

When parsing, there are two distinct cases to consider.
If a zone has been parsed directly from the text, perhaps because 
DateTimeFormatterBuilder.appendZoneId() was used, then this override Zone
has no effect. If no zone has been parsed, then this override zone will
be included in the result of the parse where it can be used to build
instants and date-times.

Side remark: The Joda-Time-method withOffsetParsed() is closer to Java-8-behaviour.

Update: I have now done my own tests. See the sometimes surprising results.

System.out.println(System.getProperty("java.version")); // 1.8.0_31

// parsing s1 with offset = UTC
String s1 = "2015-01-01T12:29:22+00:00"; 

OffsetDateTime odt1 = DATE_TIME_OPTIONAL_OFFSET.parse(s1, OffsetDateTime::from);
System.out.println(odt1); // 2015-01-01T12:29:22Z --- OK

LocalDateTime ldt1 = DATE_TIME_OPTIONAL_OFFSET.parse(s1, LocalDateTime::from);
System.out.println(ldt1); // 2015-01-01T12:29:22 --- OK

ZonedDateTime zdt1 = DATE_TIME_OPTIONAL_OFFSET.withZone(ZoneId.of("America/New_York")).parse(s1, ZonedDateTime::from);
System.out.println(zdt1); // 2015-01-01T12:29:22-05:00[America/New_York] --- seems to be a bug compared with the spec above, the parsed offset was overridden!!!

// now parsing s2 without offset
String s2 = "2015-01-01T12:29:22";

OffsetDateTime odt2 = DATE_TIME_OPTIONAL_OFFSET.parse(s2, OffsetDateTime::from);
System.out.println(odt2); // 2015-01-01T12:29:22Z --- questionable, the offset Z is invented/guessed here

LocalDateTime ldt2 = DATE_TIME_OPTIONAL_OFFSET.parse(s2, LocalDateTime::from);
System.out.println(ldt2); // 2015-01-01T12:29:22 --- OK

DATE_TIME_OPTIONAL_OFFSET.withZone(ZoneId.of("America/New_York")).parse(s2, ZonedDateTime::from);
// throws an exception --- seems to be a bug compared with the spec above, the zone set was not accepted

Conclusion:

I would be careful when migrating. The devil is in the details. Maybe a newer Java-version 8u40 has meanwhile corrected some of the problems shown (at least the behaviour of withZone() is probably corrected - see JDK-issue 8033662, but for 8u31 the backport fix appears to be missing?!). You should also note that your "timezone" labelled "EST" was replaced by "America/New_York" in my tests because "EST" is not a recognized timezone id (it is rather a localized timezone name abbreviation in US).

Update - final solution

After extra testing this code seems to work in Java 8u31 (assuming UTC as default in case of missing offset in input):

static final DateTimeFormatter DATE_TIME_OPTIONAL_OFFSET =
    DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss[xxx]");      
OffsetDateTime odt = 
  DATE_TIME_OPTIONAL_OFFSET.withZone(ZoneOffset.UTC).parse(input, OffsetDateTime::from);
ZonedDateTime zdt = odt.toZonedDateTime(); // containing a fixed offset
like image 120
Meno Hochschild Avatar answered Oct 31 '22 00:10

Meno Hochschild