Summary
Instant.parse()
method cannot parse hour-only zone offsets.
Problem
java.time.Instant.parse(CharSequence)
method fails to parse the date/time text that includes an hour-only zone offset, such as "+02". For example,
Instant .parse("2017-01-01T00:00:00.000+02")
this throws
java.time.format.DateTimeParseException: Text '2017-01-01T00:00:00.000+02' could not be parsed at index 23
Instant.parse()
method uses DateTimeFormatter.ISO_INSTANT
formatter, and its offset parsing behavior follows DateTimeFormatterBuilder.appendOffsetId()
where it reads:
This is equivalent to calling appendOffset("+HH:mm:ss", "Z")
and in appendOffset(String, String)
:
+HH:mm:ss - hour, with minute if non-zero or with minute and second if non-zero, with colon
This implies the minutes are optional, while the actual behavior is not, because the actual implementation uses "+HH:MM:ss" for parsing/formatting the zone offset.
Solution
Replacing the pattern in the implementation from "+HH:mm:ss" to "+HH:MM:ss" will accept hour-only zone offsets. However, it will also cause compatibility problem on formatting, i.e., it won't print minutes if they are zero. For example "+02:00" would print "+02" which should be avoided. Thus, it would be desirable to align the specification to the actual implementation.
Specification
Replace the pattern in DateTimeFormatterBuilder.appendOffsetId()
as follows:
* Appends the zone offset, such as '+01:00', to the formatter.
* <p>
* This appends an instruction to format/parse the offset ID to the builder.
- * This is equivalent to calling {@code appendOffset("+HH:mm:ss", "Z")}.
+ * This is equivalent to calling {@code appendOffset("+HH:MM:ss", "Z")}.
* See {@link #appendOffset(String, String)} for details on formatting
* and parsing.
*
- csr of
-
JDK-8364752 Class java.time.Instant cannot parse all ISO 8601 date formats
-
- In Progress
-