Kresimir Nesek Kresimir Nesek - 4 months ago 200
Java Question

Jackson deserialize ISO8601 fromatted date-time into Java8 Instant

I'm trying to deserialize an ISO8601 formatted date into Java8 Instant using Jackson. I registered JavaTimeModule with the ObjectMapper, turned off WRITE_DATES_AS_TIMESTAMPS.

However, if one tries to deserialize 2016-03-28T19:00:00.000+01:00 it will not work, because it seems that JavaTimeModule will only deserialize date-times formatted with UTC timezone offset (e.g. 2016-03-28T18:00:00.000Z). I then tried using @JsonFormat annotation like this:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone = "UTC")

And like this:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone = JsonFormat.DEFAULT_TIMEZONE)

However, neither of these work and I get an exception:

com.fasterxml.jackson.databind.JsonMappingException: Unsupported field: YearOfEra (through reference chain: org.example.Article["date"])

Which implies that timezone parameter is ignored and date time formatter doesn't know how to format an Instant without a timezone.

Is there a way to deserialize a ISO8601 string that's not in UTC time zone offset to Java 8 Instant using Jackson and JavaTimeModule without writing custom deserializer?


Strange, I expected this error:

Failed to parse Date value '2016-03-28T19:00:00.000+01:00' 
(format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"): 
Unparseable date: "2016-03-28T19:00:00.000+01:00" 

and after trying your code, I got this kind of error in both cases. The format is not matching... did you try it without annotations or with this pattern: yyyy-MM-dd'T'HH:mm:ss.SSSXXX? (see: