阿姆斯特丹时区 SimpleDateFormat 的行为不一致
昨天我遇到了一个问题,一个人的出生日期在使用 XStream 从 Date 编组到 xml,然后再次解组到 Date 后发生了更改。以下代码重现了 XStream 的奇怪行为:
System.setProperty("user.timezone", "Europe/Amsterdam");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z");
String textIn = "1933-09-17 00:00:00.0 CET";
Date date = dateFormat.parse(textIn);
String textOut = dateFormat.format(date);
System.out.println("input : " + textIn);
System.out.println("date : " + date);
System.out.println("output: " + textOut);
结果:
input : 1933-09-17 00:00:00.0 CET
date : Sun Sep 17 00:19:32 CEST 1933
output: 1933-09-17 00:19:32.0 CEST
我发现它只发生在 1940 年之前的日期。这在某种程度上是可以解释的:1940 年荷兰发生了所谓的“Amsterdamse Tijd”的变化(GMT+00h19m32s) 转换为欧洲时间 (GMT+01h00m00s)。我无法解释为什么时区更改为节省时间(从 CET 到 CEST)。
如果我将时区更改为柏林,
System.setProperty("user.timezone", "Europe/Berlin");
我会得到预期的结果:
input : 1933-09-17 00:00:00.0 CET
date : Sun Sep 17 00:00:00 CET 1933
output: 1933-09-17 00:00:00.0 CET
我的服务器位于阿姆斯特丹。我将服务器的时区设置为柏林,以解决该问题。
我的问题是:您认为这是 SimpleDateFormat 中的错误吗?或者代码是否无效,因为“1933-09-17 00:00:00.0 CET”对于阿姆斯特丹位置来说是无效日期?
如果这是一个错误,应该以及应该在哪里报告? 如果日期输入本身无效,那么解析方法不应该抛出错误吗?
Yesterday I ran into a problem where the date of birth of a person was changed after it was marshalled with XStream from Date to xml and then unmarshalled to Date again. The following code reproduces the strange behaviour of XStream:
System.setProperty("user.timezone", "Europe/Amsterdam");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z");
String textIn = "1933-09-17 00:00:00.0 CET";
Date date = dateFormat.parse(textIn);
String textOut = dateFormat.format(date);
System.out.println("input : " + textIn);
System.out.println("date : " + date);
System.out.println("output: " + textOut);
The results:
input : 1933-09-17 00:00:00.0 CET
date : Sun Sep 17 00:19:32 CEST 1933
output: 1933-09-17 00:19:32.0 CEST
I found out that it happens only for dates before 1940. Which is in a certain way explainable: In the Netherlands in 1940 there was a change from the so called "Amsterdamse Tijd" (GMT+00h19m32s) to European time (GMT+01h00m00s). I cannot explain why the timezone changes to saving time (from CET to CEST).
If I change the timezone to Berlin
System.setProperty("user.timezone", "Europe/Berlin");
I get the results I expect:
input : 1933-09-17 00:00:00.0 CET
date : Sun Sep 17 00:00:00 CET 1933
output: 1933-09-17 00:00:00.0 CET
My server is located in Amsterdam. I will set the timezone of the server to Berlin, to work around the problem.
My question is: Do you think this is a bug in SimpleDateFormat? Or is the code invalid because "1933-09-17 00:00:00.0 CET" is an invalid date for location Amsterdam?
If it is a bug, should and where should this be reported?
If the date input is invalid in in itself, shouldn't the parse method throw an error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来德国没有在 1933 年进行 CET 到 CEST 的转换,而荷兰则做了:
looks like germany did not do the CET to CEST transition in 1933, while netherlands did: