我有这个简单的代码:
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm yyyy-MM-dd");
DateTime dateTime = formatter.withZone(DateTimeZone.forID("America/New_York")).parseDateTime("08:30 2015-06-01");
DateTime dateTime2 = formatter.withZone(DateTimeZone.forID("America/New_York")).parseDateTime("08:30 2015-12-01");
这些是跳跃时间。当我点击 toString 方法时,我得到了这样的结果:
2015-06-01T08:30:00.000-04:00
2015-12-01T08:30:00.000-05:00
这是正确的,我们可以看到 UTC 时间偏移。但是当我调用 getHourOfDay 时,我得到了 8,而不是预期的 4/3。我究竟做错了什么?请在这里分享一些建议。
最佳答案
嗯,来自 DateTimeFormatter#withZone()
的 Javadoc:
Returns a new formatter that will use the specified zone in preference to the zone of the printed object, or default zone on a parse.
因此,您告诉格式化程序在解析和输出时使用特定时区,并且您提供的输入不包含时区,因此这是预期的结果。本质上你说的是:
- 这是一个没有时区的日期字符串,假设 America/New_York 来解析它
- 将日期转换回美国/纽约时区的字符串
这就是它所做的。
关于java - Joda 时间从 dateTime 获取正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34008154/