Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.time.ZonedDateTime.parse and iso8601?

Why does the JDK8 DateTime library seem to not parse valid iso8601 date time strings? It chokes on time zone offsets expressed like "+01" instead of "+01:00"

This works:

java.time.ZonedDateTime.parse("2015-08-18T00:00+01:00")

This throws a parse exception:

java.time.ZonedDateTime.parse("2015-08-18T00:00+01")

From the iso8601 wikipedia page:

The offset from UTC is appended to the time in the same way that 'Z' was above, in the form ±[hh]:[mm], ±[hh][mm], or ±[hh]. So if the time being described is one hour ahead of UTC (such as the time in Berlin during the winter), the zone designator would be "+01:00", "+0100", or simply "+01".

EDIT: This looks like an actual legitimate bug in the JDK.

https://bugs.openjdk.java.net/browse/JDK-8032051

Wow, after testing that new date time stuff for years, I thought they would have caught something so obvious. I also thought the JDK author types were rigorous enough to use a better automated test suite.

UPDATE: This is completely fixed in the current jdk-9 build. I just confirmed. The exact same parse command showed above fails in the current jdk-8 build and works perfectly in jdk-9.

ADDENDUM: FWIW, RFC 3339 based on ISO-8601, does not allow for this short hand. You must specify minutes in the time zone offsets.

like image 715
clay Avatar asked Aug 18 '15 17:08

clay


People also ask

How do I get time from ZonedDateTime?

now() now() method of a ZonedDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return ZonedDateTime based on system clock with default time-zone to obtain the current date-time. The zone and offset will be set based on the time-zone in the clock.

What is ISO 8601 format Java?

The Date/Time API in Java works with the ISO 8601 format by default, which is (yyyy-MM-dd) . All Dates by default follow this format, and all Strings that are converted must follow it if you're using the default formatter.

Is Java time format DateTimeFormatter thread-safe?

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

What is a ZonedDateTime in the Java 8 date and time API?

ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.


1 Answers

The code which is used is added by DateTimeFormatterBuilder.appendZoneId() which allows the formats for time zone as

For example, the following will parse:

"Europe/London"           -- ZoneId.of("Europe/London")
"Z"                       -- ZoneOffset.UTC
"UT"                      -- ZoneId.of("UT")
"UTC"                     -- ZoneId.of("UTC")
"GMT"                     -- ZoneId.of("GMT")
"+01:30"                  -- ZoneOffset.of("+01:30")
"UT+01:30"                -- ZoneOffset.of("+01:30")
"UTC+01:30"               -- ZoneOffset.of("+01:30")
"GMT+01:30"               -- ZoneOffset.of("+01:30")

You can define your own date time format to allow hour offsets, but many places in the world have fractions of an hour such as Nepal which is +05:45 and North Korea which recently changed to +08:30

like image 112
Peter Lawrey Avatar answered Oct 05 '22 06:10

Peter Lawrey