Reputation: 257
I am trying to parse an ISO8601 time string of format "yyyy-MM-dd'T'HH:mm:ssZ" using the below line for ZonedDateTime object:
ZonedDateTime date = ZonedDateTime.parse("2021-02-19T14:32:12+0000", DateTimeFormatter.ISO_ZONED_DATE_TIME);
However I am getting the below error when doing this:
java.time.format.DateTimeParseException: Text '2021-02-19T14:32:12+0000' could not be parsed at index 19
I cant imagine this is not allowed as the + symbol is a valid character for the parse. Can anyone assist on what is wrong here?
Upvotes: 0
Views: 904
Reputation: 22977
That is because See edit, it is not required in all cases. It is because the offset must contain a colon.ISO_ZONED_DATE_TIME
expects both an offset and a zone.
See the docs.
Use DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxx", Locale.ROOT)
instead. Alternatively, you could use the DateTimeFormatterBuilder
:
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_DATE_TIME)
.appendPattern("xx")
.toFormatter(Locale.ROOT);
Only reading the summary fragment of the Javadoc is apparently not enough. The zone id is not strictly required by the ISO_ZONED_DATE_TIME
, as mentioned in the comments. The second bullet point there mentions it: "If the zone ID is not available or is a ZoneOffset then the format is complete".
Upvotes: 2