从 DateTime (Joda Time) 到 Date (java.util) 的转换

发布于 2024-12-19 14:02:50 字数 175 浏览 1 评论 0原文

我将这个日期“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-12-26 14:02:51

Joda-Time DateTime.toDate() 方法转换为具有相同毫秒瞬时的 java.util.Datejava.util.Date.getDate() 方法使用本地默认时区返回其值,而 DateTime.getDayOfMonth() 使用存储在DateTime 对象。如果两个时区不同,您可能会看到差异,这解释了您观察到的 22/24。

要进一步了解,请打印 DateTime 的时区以及 java.util.Date 使用的默认 Java 区域 TimeZone.getDefault() >。

The Joda-Time DateTime.toDate() method converts to a java.util.Date with the same millisecond instant. The java.util.Date.getDate() method uses the local default time-zone to return its value, whereas DateTime.getDayOfMonth() uses the time-zone stored in the DateTime 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 zone TimeZone.getDefault() used by java.util.Date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文