New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fastjson在序列化JDK8的LocalDateTime是,无法设置日期格式. #942
Comments
请提供重现问题的testcase |
测试用例: @Test
public void testCase() throws Exception {
System.out.println("===直接序列化LocalDateTime,无论如何格式不可变化===");
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(JSON.toJSONString(dateTime));
/*期望是 yyyy-MM-dd HH:mm:ss 实际是 LocalDateTime.toString() 的格式 */
System.out.println(JSON.toJSONString(dateTime,SerializerFeature.WriteDateUseDateFormat));
System.out.println(JSON.toJSONStringWithDateFormat(dateTime,"yyyy-MM-dd HH:mm:ss"));
System.out.println("===直接序列化LocalDateTime.withNano(0),无论如何格式不可变化===");
dateTime = LocalDateTime.now().withNano(0);
System.out.println(JSON.toJSONString(dateTime));
/*期望是 yyyy-MM-dd HH:mm:ss 实际是 LocalDateTime.toString() 的格式 */
System.out.println(JSON.toJSONString(dateTime,SerializerFeature.WriteDateUseDateFormat));
System.out.println(JSON.toJSONStringWithDateFormat(dateTime,"yyyy-MM-dd HH:mm:ss"));
System.out.println("==序列化VO对象中包含LocalDateTime,无论如何格式不可变化==");
LocalDateTimeModel localDateTimeModel = new LocalDateTimeModel();
System.out.println(JSON.toJSONString(localDateTimeModel));
/*期望是 yyyy-MM-dd HH:mm:ss 实际是 LocalDateTime.toString() 的格式 */
System.out.println(JSON.toJSONString(localDateTimeModel,SerializerFeature.WriteDateUseDateFormat));
System.out.println(JSON.toJSONStringWithDateFormat(localDateTimeModel,"yyyy-MM-dd HH:mm:ss"));
System.out.println("==序列化VO对象中包含LocalDateTime.withNano(0),日期格式变化正确==");
localDateTimeModel = new LocalDateTimeModel();
localDateTimeModel.setStartDateTime(localDateTimeModel.getStartDateTime().withNano(0));
localDateTimeModel.setEndDateTime(localDateTimeModel.getEndDateTime().withNano(0));
System.out.println(JSON.toJSONString(localDateTimeModel));
System.out.println(JSON.toJSONString(localDateTimeModel,SerializerFeature.WriteDateUseDateFormat));
System.out.println(JSON.toJSONStringWithDateFormat(localDateTimeModel,"yyyy-MM-dd HH:mm:ss"));
} 所使用的model: class LocalDateTimeModel implements Serializable {
private LocalDateTime startDateTime;
private LocalDateTime endDateTime;
private Date startDate;
private Date endDate;
public LocalDateTimeModel() {
startDateTime = LocalDateTime.now();
endDateTime = LocalDateTime.now().plusWeeks(1);
startDate = new Date();
endDate = new Date(startDate.getTime()+36000000);
}
public LocalDateTime getStartDateTime() {
return startDateTime;
}
public void setStartDateTime(LocalDateTime startDateTime) {
this.startDateTime = startDateTime;
}
public LocalDateTime getEndDateTime() {
return endDateTime;
}
public void setEndDateTime(LocalDateTime endDateTime) {
this.endDateTime = endDateTime;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
} 结果: ===直接序列化LocalDateTime,无论如何格式不可变化===
"2016-12-10T11:15:33.308"
"2016-12-10T11:15:33.308"
"2016-12-10T11:15:33.308"
===直接序列化LocalDateTime.withNano(0),无论如何格式不可变化===
"2016-12-10T11:15:33"
"2016-12-10T11:15:33"
"2016-12-10T11:15:33"
==序列化VO对象中包含LocalDateTime,无论如何格式不可变化==
{"endDate":1481375733393,"endDateTime":"2016-12-17T11:15:33.393","startDate":1481339733393,"startDateTime":"2016-12-10T11:15:33.393"}
{"endDate":"2016-12-10 21:15:33","endDateTime":"2016-12-17T11:15:33.393","startDate":"2016-12-10 11:15:33","startDateTime":"2016-12-10T11:15:33.393"}
{"endDate":"2016-12-10 21:15:33","endDateTime":"2016-12-17T11:15:33.393","startDate":"2016-12-10 11:15:33","startDateTime":"2016-12-10T11:15:33.393"}
==序列化VO对象中包含LocalDateTime.withNano(0),日期格式变化正确==
{"endDate":1481375733428,"endDateTime":"2016-12-17 11:15:33","startDate":1481339733428,"startDateTime":"2016-12-10 11:15:33"}
{"endDate":"2016-12-10 21:15:33","endDateTime":"2016-12-17 11:15:33","startDate":"2016-12-10 11:15:33","startDateTime":"2016-12-10 11:15:33"}
{"endDate":"2016-12-10 21:15:33","endDateTime":"2016-12-17 11:15:33","startDate":"2016-12-10 11:15:33","startDateTime":"2016-12-10 11:15:33"}
|
还有toJavaObject的反序列化方法也存在问题,尽快修复下。 |
wenshao
added a commit
that referenced
this issue
Dec 19, 2016
1.2.23中问题已经修复,会尽快发布新版本 |
wenshao
added a commit
that referenced
this issue
Dec 19, 2016
Fastjson把对象转为json时候,Date会转为时间戳,那LocalDateTime怎么转为时间戳呢 |
同问,LocalDateTime怎么转为时间戳? |
追问,LocalDateTime怎么转换为时间戳呀 |
This was referenced Mar 12, 2021
Closed
asa1997
pushed a commit
to Be-Secure/fastjson
that referenced
this issue
Feb 8, 2023
asa1997
pushed a commit
to Be-Secure/fastjson
that referenced
this issue
Feb 8, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用的是 1.2.21 这个版本.
不管我设不设置
SerializerFeature.WriteDateUseDateFormat
或者调用JSON.toJSONStringWithDateFormat
都没有其效果. 最后生成的JSON还是 LocalDateTime.toString 的样式.翻了一下源码. 发现
这个类. 在直接调用
JSON.toJSONStringWithDateFormat(LocalDateTime.now())
的时候,传入的fieldType
为null,导致走到out.writeString(object.toString());
去了. 从而生成的json为LocalDateTime.toString的格式.而如果是在一个JAVABean中有LocalDateTime字段,进行序列化的时候. 传入的fieldType是正常的
LocalDateTime.class
. 但是if (dateTime.getNano() == 0)
这句过不到,导致还是走到out.writeString(object.toString());
中去了.从而生成的json为LocalDateTime.toString的格式.不大理解
if (dateTime.getNano() == 0)
这句话在这的目的是什么~ 只要我实例化LocalDateTime的时候使用LocalDateTime.now().withNano(0)
就可以格式化时间了. 谢谢!The text was updated successfully, but these errors were encountered: