当我们用JSON.toJSONString()来打印日志的时候发现打印出来的日期是时间戳,不够明晰,用以下方法可以将时间戳转为指定格式输出,更加易读
1.
public static void main(String[] args) {
InventoryEventAddReqVo inventoryEventAddReqVo = new InventoryEventAddReqVo();
inventoryEventAddReqVo.setEventDate(new Date());
System.out.println(JSON.toJSONString(inventoryEventAddReqVo));
String s = JSON.toJSONStringWithDateFormat(inventoryEventAddReqVo, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);
System.out.println(s);
}
{"eventDate":1642148601390}
{"eventDate":"2022-01-14"}
2.对象字段上加@JSONField(format = "yyyy-MM-dd")