1. Overview

In this article, we will discuss Spring Jackson custom date format with examples. When Date type object serializes at that time Jackson will convert to Long (Milliseconds from 1970) but sometimes we need a specific format instead of Long timestamp like readable format.

Spring provides multiple ways using that we can customize date format instead of Long timestamp while serializing JSON, XML or any other format.

This article is only about the custom date format in serializing or deserializing, If we want to write custom logic for serializing or deserializing objects then here is an article for the same.  If you are new with Spring or Spring boot then here is JSON Rest API example  and here is XML Rest API example.

1. Customize Jackson date format using @JsonFormat

@JsonFormat (com.fasterxml.jackson.annotation.JsonFormat) is annotation using that we can customize date format according to our requirement for specific date type field, here is syntax:

@JsonFormat(pattern = DATE_TIME_FORMAT [, timezone = TIME_ZONE])

Example:

 @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss Z", timezone = "America/Los_Angeles")
 private Date joinDate = new Date();

2. Customize Jackson date format using properties (Global Configuration)

@JsonFormat annotation needs to write to each and every date field in all the class but spring also provides global configuration using that we can apply date format entire application serialization.

While working with spring boot application, we can configure spring.jackson.date-formatspring.jackson.time-zone in the application.properties for global configurations:

application.properties

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=America/Los_Angeles

application.yml

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: America/Los_Angeles

3. Customize Jackson date format using ObjectMapper (Global Configuration)

Without spring boot application, In Spring MVC application we can create ObjectMapper bean, in which we can define date format and time zone based on that Jackson will convert date object in the specified format.

import org.springframework.context.annotation.Bean;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.fasterxml.jackson.databind.ObjectMapper;


    @Bean
    public ObjectMapper objectMapper() {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

        ObjectMapper dateFormatMapper = new ObjectMapper();
        dateFormatMapper.setDateFormat(dateFormat);
        return dateFormatMapper;
    }

2. Examples of Customize or change Jackson date format

1. Default date format is Long Timestamp (Default)

If we do not write any configuration then Jackson will convert the date to Long, means milliseconds from 1970 as shown in the output:

public class Employee implements Serializable{

    private int no;
    private String name;
    private String designation;
    private String gender;
    private Date joinDate = new Date();
}

Output:

[
  {
    "no": 1,
    "name": "Bob",
    "designation": "Developer",
    "gender": "Male",
    "joinDate": 1534052775702
  },
  {
    "no": 2,
    "name": "Joy",
    "designation": "Sr. Developer",
    "gender": "Male",
    "joinDate": 1534052775702
  }
]

2. Format date using @JsonFormat annotation

@JsonFormat annotation has one of the parameters called pattern in which we can pass date format so Jackson will convert the date in the specified format. Here is an official document Java date format.

NOTE: By default, It will use UTC timezone for conversation.

public class Employee implements Serializable{

    private int no;
    private String name;
    private String designation;
    private String gender;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date joinDate = new Date();
}

Output:

[
  {
    "no": 1,
    "name": "Bob",
    "designation": "Developer",
    "gender": "Male",
    "joinDate": "2018-08-12"
  },
  {
    "no": 2,
    "name": "Joy",
    "designation": "Sr. Developer",
    "gender": "Male",
    "joinDate": "2018-08-12"
  }
]

3. Format date and time @JsonFormat annotation

pattern parameter accepts a date and time format which is supported by java. Here is an official document Java date format. Here we have passed parameter related to time so it will convert date including time.

NOTE: By default, It will use UTC timezone for conversation.

public class Employee implements Serializable{

    private int no;
    private String name;
    private String designation;
    private String gender;
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date joinDate = new Date();
}

Output:

[
  {
    "no": 1,
    "name": "Bob",
    "designation": "Developer",
    "gender": "Male",
    "joinDate": "2018-08-12 06:10:19"
  },
  {
    "no": 2,
    "name": "Joy",
    "designation": "Sr. Developer",
    "gender": "Male",
    "joinDate": "2018-08-12 06:10:19"
  }
]

4. Format date time with time zone using @JsonFormat annotation

@JsonFormat annotation also accept timezone parameter which accepts timezone in which date will be converted. Here is a list of all timezone.

public class Employee implements Serializable{

    private int no;
    private String name;
    private String designation;
    private String gender;
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss Z", timezone = "America/Los_Angeles")
    private Date joinDate = new Date();
}

Output:

[
  {
    "no": 1,
    "name": "Bob",
    "designation": "Developer",
    "gender": "Male",
    "joinDate": "2018-08-11 11:29:30 -0700"
  },
  {
    "no": 2,
    "name": "Joy",
    "designation": "Sr. Developer",
    "gender": "Male",
    "joinDate": "2018-08-11 11:29:30 -0700"
  }
]

5. Format date time with time zone using application.properties

Using application.properties we can set a global setting which will be affected to all the date serialization. spring.jackson.date-format accept valid date format and spring .jackson.time-zone accept valid timezone.

application.properties

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=America/Los_Angeles

Output:

[
  {
    "no": 1,
    "name": "Bob",
    "designation": "Developer",
    "gender": "Male",
    "joinDate": "2018-08-12 07:12:22"
  },
  {
    "no": 2,
    "name": "Joy",
    "designation": "Sr. Developer",
    "gender": "Male",
    "joinDate": "2018-08-12 07:12:22"
  }
]

3. Conclusion

In this article, We learned ways to change Date format in JSON or XML rest API response. Here we can only specify the date format but not able to write custom code to convert Date object with any other specific business logic, Here we have written article for customized the business logic to serialize and deserialize objects. We also learn that we can also specify timezone while serializing Date object.

4. References

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *