java - Joda 时间从 dateTime 获取正确的时间

标签 java datetime timezone jodatime

我有这个简单的代码:

    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.

因此,您告诉格式化程序在解析和输出时使用特定时区,并且您提供的输入不包含时区,因此这是预期的结果。本质上你说的是:

  1. 这是一个没有时区的日期字符串,假设 America/New_York 来解析它
  2. 将日期转换回美国/纽约时区的字符串

这就是它所做的。

关于java - Joda 时间从 dateTime 获取正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34008154/

相关文章:

python - 保留最新的值并删除旧的行( Pandas )

c# - 如何将UTC时间转换为C#中任何其他时区的时间

java - 使用定时器在java中安排任务

java - jedis psubscribe 导致应用挂起

Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException : Unrecognized field "" not marked as ignorable

java - 垂直对齐 BoxLayout 间距?

python - datetime64[ns] 与日期的比较

python - 在 Python 中将 N 秒添加到 datetime.time 的标准方法是什么?

python时间模块: daylight savings

java - 工厂和泛型