Search code examples
javajava-8iso8601

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.


Solution

  • 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.

    Using the new jdk-9 shell:

    ➜  jdk-9 bin/jshell
    |  Welcome to JShell -- Version 9-ea
    |  For an introduction type: /help intro
    
    jshell> java.time.ZonedDateTime.parse("2015-08-18T00:00+01")
    $1 ==> 2015-08-18T00:00+01:00