问题复现代码如下 :
// 这个配置好像并不生效
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
TestBean test = new TestBean();
LocalDateTime time1 = LocalDateTime.now();
LocalDateTime time2 = LocalDateTime.parse("2017-09-22 15:08:56", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
test.setTime1(time1);
test.setTime2(time2);
String json = JSON.toJSONString(test, SerializerFeature.WriteDateUseDateFormat);
System.out.println(json);
output : {"time1":"2017-09-22T15:19:53.359","time2":"2017-09-22 15:08:56"}
输出的jsonString 会出现不同的格式。 这使的我在反序列化时出现困扰,需要为不同的格式制定不同的DateTimeFormatter. 目前只知道 jsonString 直接反序列化为对象时,是没有问题的,LocalDateTime 类型的字段会被正确赋值。 但是很多时候只是想取出jsonString 中的某个String 进行解析处理。我想知道这是否是一个BUG。
下面是我使用的版本
com.alibaba fastjson 1.2.17