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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With