数据库存储的数据如下,格式为yyyy-MM-dd hh:mm:ss

封装实体类后,日期数据格式为:
birthday=Tue Feb 27 17:47:08 CST 2018
可以看出,日期格式没有问题,但是在经过该代码后,日期数据格式被jackson自动替换了
String s = JSONObject.toJSONString(pojo);
//pojo是接收的实体类对象
结果为:
"birthday":1519724828000
解决方法
toJSONString方法为我们提供了一个参数:
SerializerFeature.WriteDateUseDateFormat
改造上面的代码为:
String s = JSONObject.toJSONString(pojo, SerializerFeature.WriteDateUseDateFormat);
SerializerFeature.WriteDateUseDateFormat默认的日期格式为yyyy-MM-dd hh:mm:ss
再次执行,日期格式正确
"birthday":"2018-02-27 17:47:08"
本文讲述了如何在使用Jackson将实体类中的日期字段转换为ISO-8601格式的问题,通过SerializerFeature.WriteDateUseDateFormat属性解决了日期输出格式不一致的问题,确保了日期信息的正确显示。
1436





