How to make JsonGenerator pretty-print Date and DateTime values?

前端 未结 2 854
慢半拍i
慢半拍i 2021-02-15 00:03

I\'m using this method to convert any object to a json string:

private String objectToJson(Object object) throws IOException {
        // write JSON
        Stri         


        
2条回答
  •  执念已碎
    2021-02-15 00:55

    Because the epoch timestamp (number of milliseconds since January 1st, 1970, UTC) is the most efficient and accurate representation of the time, nearly all date and time related objects are serialized to it. You can of course overwrite it and if you want to use the format mentioned in the question, you'd need to add the following to your code:

    DateFormat myDateFormat = new SimpleDateFormat("hh:mm:ss");
    objectMapper.getSerializationConfig().setDateFormat(myDateFormat);
    objectMapper.getDeserializationConfig().setDateFormat(myDateFormat); 
    

    For more information regarding dates and times in the Jackson JSON-processor see the following link: http://wiki.fasterxml.com/JacksonFAQDateHandling

提交回复
热议问题