Jackson deserialize ISO8601 formatted date-time into Java8 Instant

后端 未结 5 1960
傲寒
傲寒 2021-02-03 18:02

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

相关标签:
5条回答
  • 2021-02-03 18:29

    In Jackson 2.9.8 (current one as I'm writing this) it's better to use Instant instead of Date.

    You have to add a dependency:

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.9.8</version>
    </dependency> 
    

    Also, register the module and configure SerializationFeature.WRITE_DATES_AS_TIMESTAMPS to false.

    new ObjectMapper()
                    .findAndRegisterModules()
                    .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    

    More information about Jackson for Java8 here: https://github.com/FasterXML/jackson-modules-java8

    0 讨论(0)
  • 2021-02-03 18:32

    The format "Z" does not work with "+01:00" as this is a different pattern. JsonFormat is using SimpleDateFormat patterns. "Z" in upper case only represents strict RFC 822. You have to use syntax like: "+0100", without colon.

    See: ISO 8601:2004, SimpleDateFormat patterns

    0 讨论(0)
  • 2021-02-03 18:34

    You need to set the explicit time zone via XXX in your modell class:

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

    (see: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)

    0 讨论(0)
  • 2021-02-03 18:49

    Jackson can be configured globally (without annotations) to accept timestamps with or without colon:

    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
    

    The default Jackson timezone format was changed since version 2.11 from '+0000' to '+00:00'. Both formats are valid according to ISO-8601.

    0 讨论(0)
  • 2021-02-03 18:51

    If you want to serialize Date objects into ISO-8601, you don't need to specify a pattern at all - ISO-8601 is the default pattern. It is kind of mentioned in the JsonFormat Java doc:

    Common uses include choosing between alternate representations -- for example, whether Date is to be serialized as number (Java timestamp) or String (such as ISO-8601 compatible time value) -- as well as configuring exact details with pattern() property.

    [emphasasis mine] you should understand from the above text that specifying shape = STRING would mean an ISO-8601 format but you can choose something else using the pattern property.

    In my experience, this always turns out a UTC date format (with the time zone rendered as +0000), which could be the default time zone in my VM (even though my operating system clock is not set to UTC).

    0 讨论(0)
提交回复
热议问题