Jackson deserialize ISO8601 formatted date-time into Java8 Instant

We Are Going To Discuss About Jackson deserialize ISO8601 formatted date-time into Java8 Instant. So lets Start this Java Article.

Jackson deserialize ISO8601 formatted date-time into Java8 Instant

  1. Jackson deserialize ISO8601 formatted date-time into Java8 Instant

    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.

  2. Jackson deserialize ISO8601 formatted date-time into Java8 Instant

    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.

Solution 1

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)

Original Author Meiko Rachimow Of This Content

Solution 2

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).

Original Author Guss Of This Content

Solution 3

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

Original Author zoomout Of This Content

Solution 4

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

Original Author Mick Belker – Pseudonym Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment