Search code examples
javajodatimejira-rest-apizoneddatetimelocaldatetime

How to parse ZonedDateTime with milliseconds


Jira is giving me this date format via Rest API:
2021-01-21T11:08:45.000+0100

How can i parse this to a LocalDateTime in Java?

I tried

ZonedDateTime.parse("2021-01-21T11:08:45.000+0100", DateTimeFormatter.ISO_OFFSET_DATE_TIME);

Or this:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
ZonedDateTime.parse("2021-01-21T11:08:45.000+0100"), formatter);

The result is DateTimeParseException


Solution

  • Since the zone offset in your value is in the format +0100, it cannot be parsed with any of the predefined formatters like DateTimeFormatter.ISO_OFFSET_DATE_TIME, as it expects it to be in the format +01:00

    You can parse 2021-01-21T11:08:45.000+0100 with the pattern "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

    ZonedDateTime.parse("2021-01-21T11:08:45.000+0100", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"))
    

    The reference for DateTimeFormatter is here.