数据库存储的数据如下,格式为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"

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:21 天前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐