Serialize Date by using Jackson
In Java, Date is one of the most important data types. When we work with REST API, we need to serialize Date. In this section, we will understand how we can serialize Date by using Jackson. We will understand how we can serialize the following types of dates:
- util.Date
- Joda-Time
- Java 8 DateTime
Let’s understand each one of them one by one:
Serialize Date to Timestamp
In order to serialize the “java.util.Date“, we take the help of the Event class. We first create a date by using SimpleDateFormat class. After that, we create an instance of an Event by using the date field. Now, we serialize the Event instance by using the ObjectMapper class.
Let’s implement the code for the above theory:
SerializeDateToTimestamp.java
Output:
Serialize Date to ISO-86o1
Just like Date to Timestamp serialization, we can also serialize Date to ISO-86o1 by using the ObjectMapper class. We use the setDateFormat() method of the ObjectMapper class to serialize Date to ISO-86o1.
Let’s take an example to understand how serialization is done by using ObjectMapper class:
SerializeDateToISO.java
Output:
Configure ObjectMapper DateFormat
All the previous ways of Date serialization still lack the full flexibility of choosing the exact format for representing the java.util.Date instance. Now, we perform some configurations which allow us to set our formats for representing dates.
Let’s take an example to configure the ObjectMapper for allowing us to set Date format.
SerializeDateWithConfiguration.java
Output:
Serialize Date with @JsonFormat
We can also serialize the Date by using the @JsonFormat annotation. We use annotation for controlling the Date format on individual classes. So, we use annotation when we don’t want to control the date format globally.
Let’s take an example to understand how we can use annotation for controlling Date format.
SerializeDateWithJsonFormat.java
Output:
Serialize Date with Custom Date Serializer
It is another way of serializing the Date field. We define the custom date serializer, and then we use it with the @JsonSerialize.
Let’s first define the Date serializer, and then we take an example in which we use our custom date serializer.
CustomDateSerializer.java
Output:
Serialize Joda-Time with Jackson
The “java.util.Date” is not the only class that provides a Date instance. Dates can be represented by other classes also, such as Joda-Time, etc. DateTime is one of the most common implementations from the Joda-Time library.
In order to use the Joda-Time, we add the following dependency in our POM.xml file:
To serialize Joda-Time, we simply register the JodaModule to the ObjectMapper class:
SerializeJodaTimeExample.java
Output:
Serialize Joda-Time with Custom Serializer
Just like “java.util.Date“, we can also serialize the Joda-Time by using the DateTime serializer. Let’s first create Custom DateTime Serializer, and then we will use it to serialize Joda-DateTime.
CustomDateTimeSerializer.java
SerializeJodaTimeWithSerializer.java
Output:
Serialize Java 8 Date with Jackson
Just like java.util.Date and Joda-DateTime, we can also serialize Java 8 Date by using Jackson. We will use LocalDateTime for serialization and for that we add the following dependency in our POM.xml file.
In order to serialize Java 8 Date, we will register the JavaTimeModule to ObjectMapper class.
Let’s take an example to understand how the serialization of Java 8 Date is done by using Jackson.
SerializeJava8DateExample.java
Output:
Serialize Java 8 Date with Custom Serializer
We can also serialize Java 8 Date by using Custom LocalDateTime Serializer. Just like java.util.Date and Joda-DateTime, we create a custom serializer, and then we use it in our code to serialize LocalDateTime in Java.
CustomLocalDateTimeSerializer.java
SerializeJava8DateWithCustomSerializer.java
Output: