有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Joda DateTime toDate返回的不是创建的DateTime

嗨,我注意到使用Zone创建的Joda DateTime正在按照当前系统日期返回toDate。我希望toDate能按照创建的日期时间返回。根据Joda API,它说,toDate应该返回用这个日期时间初始化的日期。我在Maven中使用了Joda 2.9.4。古鲁有什么想法吗

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

共 (0) 个答案