Use java.time in Jackson


Jackson will convert java.util.Date and java.util.Time nicely and the output will be in yyyy-MM-dd, HH: MM: ss and other formats. On the other hand, the Local ~ class of the java.time package added after Java 8 does not serialize well, and it is processed as an associative array as follows.

(The following source code is inconsistent because it uses the execution time to get the date and time.)


With this, there is a lot of unnecessary information, and the recipient will be in trouble, so I would like to do the following.

{"id":1,"name":"hoge","registrationDateTime":"2018/09/19 10:29:30"}


First, you need to explicitly register the following modules in the ObjectMapper instance so that Jackson can handle java.time.

By using this class as follows, java.time will be serialized to some extent in an easy-to-understand manner.

ObjectMapper om = new ObjectMapper();
JavaTimeModule jtm = new JavaTimeModule();

As a result, it will be serialized as follows.


This is still awkward, so we need to change the default format.

Any format can be specified as follows.

jtm.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")));

As a result, it will be serialized as follows.

{"id":1,"name":"hoge","registrationDateTime":"2018/09/19 10:29:30"}

This time I'm focusing on serialization, but when deserializing the date and time to JavaObject using java.time, an exception will occur if the format is yyyy / MM / dd, so it is the same as the procedure for registering Serializer. You also need to register the Desirializer in as follows.

jtm.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")));

Source code

The source code used for the explanation is listed in git. Please try it.

Recommended Posts

Use java.time in Jackson
Use Interceptor in Spring
Use OpenCV in Java
Use MouseListener in Processing
Use images in Rails
Use PostgreSQL in Scala
Use PreparedStatement in Java
Use ruby variables in javascript.
Use Redis Stream in Java
Use multiple checkboxes in Rails6!
How to use Lombok in Spring
Use voice recognition function in Unity
Use constructor with arguments in cucumber-picocontainer
Let's use Twilio in Java! (Introduction)
[Rails] Use cookies in API mode
[Java] Do not use "+" in append!
Use composite keys in Java Map.
Use the Findbugs plugin in Eclipse
How to use InjectorHolder in OpenAM
How to use classes in Java?
Use completion in Eclipse on mac
Address already in use workaround (Windows)
Do you use Stream in Java?
[Cloud9] Address already in use [Solution]
JSON in Java and Jackson Part ③ Embed JSON in HTML and use it from JavaScript
Multilingual Locale in Java How to use Locale
Use OpenCV_Contrib (ArUco) in Java! (Part 2-Programming)
Do not use instance variables in partial
[Docker] Use environment variables in Nginx conf
Studying Java 8 (date API in java.time package)
Use inequality comparison operators in MyBatis SQL
Use docker in proxy environment on ubuntu 20.04.1
[Java] Use cryptography in the standard library
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
Use "Rhino" which runs JavaScript in Java
Use selenium (Firefox) in Ruby in WSL environment
Use DynamoDB query method in Spring Boot
Use selenium (Chrome) in Ruby in WSL environment
How to use environment variables in RubyOnRails
I want to use @Autowired in Servlet
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to assemble JSON directly in Jackson
Steps to Use JConsole in Cognos Analytics
How to use ExpandableListView in Android Studio