Rahul Mittal Rahul Mittal - 4 months ago 27x
JSON Question

Jackson date format issue : java bean to JSON string conversion

I am converting a java bean to JSON string using Jackson 2.7.4 version. In doing so, I am facing date format issue. Java 1.7 version is being used.

Bean :

public class BaseBean {

private java.util.Date fromDate;

public Date getFromDate() {
return fromDate;

public void setFromDate(Date fromDate) {
this.fromDate = fromDate;

and I am getting below date format


Which is not required by me. then I configured below code

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS , false);
jsonInString = objectMapper.writeValueAsString(objJava);

Then I am getting below date format in JSON :


Expected date format :


Is there any configuration to handle it and get expected date format in JSON string.


JSON date Serializer :

public class CustomDateSerializer extends JsonSerializer<Date> {

public void serialize(Date date, JsonGenerator jgen, SerializerProvider provider) throws IOException,
        JsonProcessingException {

    // below methods of Date object is deprecated - consider this as sample example 
    int idate = date.getDate();
    int day = date.getDay();
    int hours = date.getHours();
    int minutes = date.getMinutes();
    int month = date.getMonth();
    int seconds = date.getSeconds();
    long time = date.getTime();
    int timezoneOffset = date.getTimezoneOffset();
    int year = date.getYear();


    jgen.writeNumberField("date", idate);
    jgen.writeNumberField("day", day);
    jgen.writeNumberField("hours", hours);
    jgen.writeNumberField("minutes", minutes);
    jgen.writeNumberField("month", month);
    jgen.writeNumberField("seconds", seconds);
    jgen.writeNumberField("time", time);
    jgen.writeNumberField("timezoneOffset", timezoneOffset);
    jgen.writeNumberField("year", year);


public Class<Date> handledType() {
    return Date.class;


Setting serializer in SimpleModule :

ObjectMapper objectMapper = new ObjectMapper();
    String jsonInString = "";
    try {
        SimpleModule myModule = new SimpleModule();

        myModule.addSerializer(Date.class, new CustomDateSerializer());
        jsonInString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objJava);
    } catch (JsonProcessingException e1) {          

Hope this will help !