Jackson custom date serializer

In this post we will provide an example on Jackson custom date serializer. Jackson is a JSON processor library for java. It allows you to convert a java POJO to and from its JSON representation along with many other things.


You will need to add the following dependancy

Add the following class if you wish to follow the examples below:

Jackson custom date serializer

Example 1

The first example shows the default behaviour when you convert a Java object with a date field to JSON (also known as serialization).

As you can see from the output below, the default behaviour is to represent the date as a timestamp in milliseconds.

Example 2

To have a custom serializer for date fields you will need to

  • Create a custom Serializer by extending the StdSerializer class
  • Register a new module with the ObjectMapper object.

Below is our custom date serializer, it writes dates as a timestamp in seconds not milliseconds like the default behaviour.

The code snippet below converts an Employee object to JSON using the custom date serializer.

Running the above code produces the following output. As you can see the timestamp does not contain the milliseconds.

Example 3

The final example is when you want to represent the date field as a string in a particular format such as dd-MM-yyy or yyyy-MM-dd etc… This is a lot simpler as the ObjectMapper has a method allowing you to set the date format.

Running the above code snippet produces the following output:

Similar posts

  1. JSON array to list using Jackson
  2. Java object to JSON using Jackson
  3. Java 8 Functional Interface
  4. How to reverse a String in Java


  1. Jackson Documentation


Leave a Reply