Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

序列化LocalDateTime时,nanoOfSecond为0时,序列化格式不同 #1450

Open
lialun-xm opened this issue Sep 1, 2017 · 5 comments
Open
Labels

Comments

@lialun-xm
Copy link

nanoOfSecond为0时:
LocalDateTime localDateTime = LocalDateTime.of(2018, 8, 31, 15, 26, 37, 0);
JSON.toJSONString(localDateTime);//2018-08-31 15:26:37
localDateTime.toString();//2018-08-31T15:26:37

nanoOfSecond不为0时:
LocalDateTime localDateTime = LocalDateTime.of(2018, 8, 31, 15, 26, 37, 1);
JSON.toJSONString(localDateTime).toString();//2018-08-31T15:26:37.000000001
localDateTime.toString();//2018-08-31T15:26:37.000000001

@wenshao
Copy link
Member

wenshao commented Sep 1, 2017

有问题么?

@wenshao wenshao modified the milestones: 1.2.38, 1.2.39 Sep 1, 2017
@kimmking
Copy link
Contributor

kimmking commented Sep 2, 2017

我也没看懂,什么问题。

@jarhf
Copy link

jarhf commented Sep 4, 2017

有问题,Jdk8DateCodec类中判断了,如果LocalDateTime类型,getNano() == 0的话是使用的 DEFFAULT_DATE_FORMAT 或者配置的format, 否则使用的是LocalDateTime自己的toString()方法。
但是DEFFAULT_DATE_FORMAT 序列化出的日期和时间中间不带“T”。
这带来的后果是,mysql数据库,datetime, datetime(3)类型的字段序列化出的格式不一致, 而IE浏览器的不支持new Date("2017-10-01 12:12:12") 这种格式,IE要求时间字符串中间必须带T
@kimmking @wenshao

@jarhf
Copy link

jarhf commented Sep 4, 2017

还有这个问题在 1.2.11版中是不存在,因为Jdk8DateCodec.write 方法对所有类型都是调用 toString() 方法来序列化,但是为什么后续版本对LocalDateTime 类型进行了特殊判断,结果就造成了格式不一致的问题,求解?

@wenshao wenshao added bug and removed needs info labels Sep 8, 2017
@wenshao wenshao modified the milestones: 1.2.39, 1.2.40 Oct 1, 2017
wenshao added a commit that referenced this issue Oct 1, 2017
@wenshao
Copy link
Member

wenshao commented Oct 1, 2017

LocalDateTime localDateTime = LocalDateTime.of(2018, 8, 31, 15, 26, 37, 1);
String json = JSON.toJSONStringWithDateFormat(localDateTime, "yyyy-MM-dd HH:mm:ss");//2018-08-31T15:26:37.000000001
assertEquals("\"2018-08-31 15:26:37\"", json);

建议这样写

@wenshao wenshao closed this as completed Oct 1, 2017
@wenshao wenshao added question and removed bug labels Oct 1, 2017
@wenshao wenshao reopened this Oct 1, 2017
@wenshao wenshao removed this from the 1.2.39 milestone Oct 1, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants