Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java DateTimeFormatter.ISO_OFFSET_DATE_TIME Returns different values in Java 9+ compared to Java 8

Tags:

java

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.

like image 925
Arcjc Avatar asked Jan 07 '20 10:01

Arcjc


People also ask

What is the role of the DateTimeFormatter type in Java?

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.

Is Java Time format DateTimeFormatter thread-safe?

Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.

What is DateTimeFormatter Iso_date_time?

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.

What is iso_instant?

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 .


1 Answers

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

like image 161
Michael Avatar answered Nov 14 '22 23:11

Michael