Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Map Multiple Date Formats Using Jackson in Java



When we are working with those Java applications that handle JSON data, it is common that we find different date formats. It might happen that one field contains just the date, while another includes time and date, and even time zone. For handling this type of data Jackson library is very helpful.

Jackson is a popular Java library that is used to convert Java objects into JSON and vice versa. And this process is called serialization and deserialization. To handle multiple date formats in a single class, Jackson provides an annotation called @JsonFormat.

The@JsonFormat

The @JsonFormat annotation helps Jackson know how a specified field should be formatted when reading or writing from JSON format. The @JsonFormat has parameters, and they are -

  • Shape: This is a format to use (like text or number). For dates, this is usually set to JsonFormat.Shape.STRING.

  • Pattern: It is the date format you want to use (similar to SimpleDateFormat).

  • Timezone: It is a timezone applied during formatting (e.g., IST, GMT, etc.).

Date Format with @JsonFormat Annotation

This defines where you are allowed to use @JsonFormat.

@Target(value={ANNOTATION_TYPE,FIELD,METHOD,PARAMETER,TYPE})
@Retention(value=RUNTIME)
public @interface JsonFormat

Example

Let's look at an example where we define two date fields in a class. One follows the format yyyy-MM-dd, and the other includes time and time zone using yyyy-MM-dd HH:mm a z.

In the program, Jackson reads the date formats from @JsonFormat annotations and uses them to parse each field correctly. In this way, even if your JSON has different date formats, Jackson maps them properly into a Java object.

import java.io.*;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDateformatTest {
   final static ObjectMapper mapper = new ObjectMapper();
   public static void main(String[] args) throws Exception {
      JacksonDateformatTest jacksonDateformat = new JacksonDateformatTest();
      jacksonDateformat.dateformat();
   }
   public void dateformat() throws Exception {
      String json = "{"createDate":"1980-12-08"," + ""createDateGmt":"1980-12-08 3:00 PM GMT+1:00"}";
      Reader reader = new StringReader(json);
      Employee employee = mapper.readValue(reader, Employee.class);
      System.out.println(employee);
   }
}
// Employee class
class Employee implements Serializable {
   @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "IST")
   private Date createDate;
   @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm a z", timezone = "IST")
   private Date createDateGmt;
   public Date getCreateDate() {
      return createDate;
   }
   public void setCreateDate(Date createDate) {
      this.createDate = createDate;
   }
   public Date getCreateDateGmt() {
      return createDateGmt;
   }
   public void setCreateDateGmt(Date createDateGmt) {
      this.createDateGmt = createDateGmt;
   }
   @Override
   public String toString() {
      return "Employee [\ncreateDate=" + createDate + ", \ncreateDateGmt=" + createDateGmt + "\n]";
   }
}

Output

Employee [
   createDate=Mon Dec 08 00:00:00 IST 1980,
   createDateGmt=Mon Dec 08 07:30:00 IST 1980
]
Updated on: 2025-04-21T16:18:18+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements