8

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

{"fromDate":1465370289436}

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 :

{"fromDate":"2016-06-08T07:47:06.636+0000"}

Expected date format :

{"fromDate":{"date":8,"day":3,"hours":12,"minutes":48,"month":5,"seconds":9,"time":1465370289436,"timezoneOffset":-330,"year":116}}

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

3 Answers 3

7

JSON date Serializer :

public class CustomDateSerializer extends JsonSerializer<Date> {

@Override
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.writeStartObject();

    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);

    jgen.writeEndObject();      
}

@Override
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());
        objectMapper.registerModule(myModule);
        jsonInString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objJava);
    } catch (JsonProcessingException e1) {          
        e1.printStackTrace();
    }

Hope this will help !

Sign up to request clarification or add additional context in comments.

Comments

0

You can add methods to your class to get:

  • date
  • day
  • hours
  • minutes
  • ...

For example

private DateFormat hoursDF = new SimpleDateFormat("HH");

public String getHours() {
    return hoursDF.format(fromDate);
}

1 Comment

this approach is not feasible for me. Could you please advise other solution for same.
0

Annotate with JsonSerialize. It may be possible to override serialize method of JsonSerializer.

1 Comment

@JsonSerialize (using = MySerializer.class) private java.util.Date fromDate; class MySerializer extends JsonSerializer<T> { Override public void serialize(T value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(//whatever format is required here...) } } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.