Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of joda.ISODateTimeFormat in java.time?

I have the following joda date parser:

ISODateTimeFormat.dateTimeParser().withOffsetParsed().

I'd like to refactor this to java.time api. But what is the exact equivalent to the parser above, especially regarding the offset?

like image 293
membersound Avatar asked Jan 05 '23 17:01

membersound


1 Answers

The best equivalent should be this constant in package java.time.format which prefers the parsed offset according to the documentation (like the behaviour when Joda-withOffsetParsed() is used):

DateTimeFormatter.ISO_OFFSET_DATE_TIME

However, there are still small differences. The decimal separator must be a dot in Java-8 (comma not tolerated although valid and even recommended in ISO-paper). Also: Java-8 manages nanosecond precision in contrast to Jodas millisecond precision. And maybe most important difference: If the offset is missing in your input then Java-8 throws an exception but Joda not (and applies the default time zone).

About choice of type: Since you are working with DateTime and fixed offsets the best equivalent should be here OffsetDateTime in Java-8. Example of migration:

DateTime dt = ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime(input);

=>

OffsetDateTime odt = OffsetDateTime.parse(input, DateTimeFormatter.ISO_OFFSET_DATE_TIME); 
like image 73
Meno Hochschild Avatar answered Jan 16 '23 21:01

Meno Hochschild