【发布时间】:2019-10-04 10:38:49
【问题描述】:
您好,我注意到使用 Zone 创建的 Joda DateTime 根据当前系统日期返回 toDate。我期待 toDate 根据 Created DateTime 返回。根据 Joda API,它说 toDate 应该返回使用此日期时间初始化的日期。我在我的 Maven 中使用 Joda 2.9.4。来自大师的任何想法都在 SO
public class TestJodaError {
public static void main(String[] args) {
DateTime ldt = new DateTime();
System.out.println("Local dt: " + ldt + " toDate: " + ldt.toDate());
String tzId = "Asia/Singapore";
ZoneId zoneId = ZoneId.of(tzId);
ZonedDateTime zdt = ZonedDateTime.now(zoneId);
DateTimeZone dtZone = DateTimeZone.forID(zdt.getZone().getId());
DateTime rdt = new DateTime(dtZone);
System.out.println("Remote dt: " + rdt + " toDate: " + rdt.toDate());
}}
结果是
Local dt: 2019-05-17T11:33:30.333+05:30 toDate: Fri May 17 11:33:30 IST 2019
Remot dt: 2019-05-17T14:03:30.738+08:00 toDate: Fri May 17 11:33:30 IST 2019
预期结果
Remot dt: 2019-05-17T14:03:30.738+08:00 toDate: Fri May 17 14:03:30 SGT 2019
【问题讨论】:
-
toDate 是你的自定义函数吗?我在 Joda 中找不到它DateTime
-
无需Zoda时间,使用Java 8即可实现。
-
@BilboBaggins 也许实际上是
LocalDateTime? -
@AndyTurner 不确定,我一直在使用 Java 8、java.time 而不是 Joda time。只是想解决这个问题。 :)
-
toDate 方法来自 Joda DateTime
标签: java datetime timezone jodatime