When running the below code I get different behaviour with different versions of the JDK:
In Java 8 I get:
2020-01-07T09:34:38.994Z
In Java 11 I get:
2020-01-07T09:37:05.55126Z
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class MyClass {
public static void main(String args[]) {
ZonedDateTime now = ZonedDateTime.now();
DateTimeFormatter isoOffsetDateTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String format = isoOffsetDateTime.format(now);
System.out.println(format);
}
}
Running in https://www.jdoodle.com/online-java-compiler/ just to make it easier to swap JDKs quickly
Is this change documented anywhere as I couldn't find anything and/or does anyone know why it's happening ? I spotted this as the DateTimeFormatter.ISO_OFFSET_DATE_TIME
is the default Jackson formatter for a ZonedDateTime
.
The DateTimeFormatter class is used to both parse and format dates according to specified Date and Time Patterns. Use parse(...) method to convert from String to Date/Time classes, use format(...) method to convert from Date/Time into String.
Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.
ISO_DATE_TIME. The ISO-like date-time formatter that formats or parses a date-time with the offset and zone if available, such as '2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'. static DateTimeFormatter.
The ISO_INSTANT formatter is a special case formatter designed to work with Instant . If you are using a ZonedDateTime you should use a different formatter, such as ISO_DATE_TIME or ISO_ZONED_DATE_TIME .
The behaviour of the formatter hasn't changed, but the thing you're formatting has.
The precision of datetimes returned by now()
methods increased. JDK-8068730
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