为什么“12:00”是转换为 UTC 变为“11:00”?
我认为 2011-10-23 12:00:00
将与 UTC
保持相同,并且 转换日期
将是 <代码>2011-10-23 17:00:00。
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = formatter.parse("2011-10-23 12:00:00");
LocalDateTime ldt = new DateTime(dt).withZone(DateTimeZone.UTC).toLocalDateTime();
LOGGER.warn("Original date: " + ldt.toDateTime().toDate().toString());
DateTime cvtldt = ldt.toDateTime(DateTimeZone.forID("-05:00"));
LOGGER.warn("Converted date: " + cvtldt.toLocalDateTime().toDateTime().toDate().toString());
我不明白为什么输出是负一小时?
Original date: Sun Oct 23 11:00:00 BST 2011
Converted date: Sun Oct 23 11:00:00 BST 2011
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用
Date.toString()
,它始终使用本地时区。看看你的字符串如何包含“BST”?理想情况下,尽可能多地坚持仅 Joda 时间:
日期
,除非您需要Date.toString()
;您无法控制其格式。目前尚不清楚您真正想要实现的目标是什么,但几乎可以肯定您不想进行这么多转换。例如,您在调用
toLocalDateTime()
后再次调用toDateTime()
- 这意味着在您仔细指定 UTC 后,它正在使用系统默认时区之前的转换...您的代码包含以下转换(按此顺序):
您认为所有这些转换既必要又正确指定的可能性有多大? ;)
You're using
Date.toString()
which always uses the local time zone. See how your string contains "BST"?Ideally, stick to just Joda Time for as much of the time as you can:
Date
unless you need toDate.toString()
if you can possibly avoid it; you have no control over its format.It's not clear what you're really trying to achieve, but you almost certainly don't want to do this many conversions. For example, you're calling
toLocalDateTime()
followed bytoDateTime()
again - which means it's using the system default time zone, after you'd carefully specified UTC in the previous conversion...Your code contains the following conversions (in this order):
What do you think the chances of all those conversions being both necessary and correctly specified are? ;)
原始版本已更改,因为到 UTC 的转换是根据主机时区完成的,因此它应该更改。
转换后发生了变化,其实就是访问方式的问题。
您正在获取基准时间,并且修改器存储在其他字段中。
尝试进入调试模式,你会看到转换后的 cvtldt
具有带有修饰符的 toString。
问候,
格热西克
Original has changed because conversion to the UTC is done with respect to HOST time zone, so it should change.
Converted had changed, in fact it's the problem of accessing method.
You are getting base time and the modifier is stored in other field.
Try going into debugging mode and you will see that after conversion cvtldt
has toString with modifier.
Regards,
Grzesiek