从 DateTime (Joda Time) 到 Date (java.util) 的转换
我将这个日期“22/11/11”解析为名为 s 的 DateTime 对象。当我执行 s.getDayOfMonth() 时,它给了我 22 这是正确的。但是,当我将 DateTime 对象转换为 Date 对象并尝试使用 s.toDate().getDate() 获取日期时,它返回 24,这是不正确的。有谁知道为什么会发生这种情况?
I parse this date "22/11/11" into a DateTime object called s. When I do s.getDayOfMonth() it gives me 22 which is right. However, when I convert the DateTime object to a Date object and try to get the date using s.toDate().getDate() it returns 24 which is not right. Does anyone have an idea why is this happening ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Joda-Time
DateTime.toDate()
方法转换为具有相同毫秒瞬时的java.util.Date
。java.util.Date.getDate()
方法使用本地默认时区返回其值,而DateTime.getDayOfMonth()
使用存储在DateTime
对象。如果两个时区不同,您可能会看到差异,这解释了您观察到的 22/24。要进一步了解,请打印
DateTime
的时区以及java.util.Date
使用的默认 Java 区域TimeZone.getDefault()
>。The Joda-Time
DateTime.toDate()
method converts to ajava.util.Date
with the same millisecond instant. Thejava.util.Date.getDate()
method uses the local default time-zone to return its value, whereasDateTime.getDayOfMonth()
uses the time-zone stored in theDateTime
object. If the two time-zones are different, you may see a difference, explaining the 22/24 you observe.To understand further, print the time-zone of the
DateTime
, and the default Java zoneTimeZone.getDefault()
used byjava.util.Date
.