如何将乔达时间中的日期时间从时区 A 转换为时区 B?

How can I convert date time from time zone A to time zone B in Joda time?

假设我有一些来自一个系统的日期时间值,以及来自另一个系统的时区信息(源和目标时区)。我需要将日期时间从源时区转换为目标时区。

例如,假设我得到一个莫斯科时区的日期时间。我需要将其转换为柏林时区。我怎样才能使用 Joda Time 做到这一点?

private static final String TIMEZONE_BERLIN_ID = "Europe/Berlin"; //$NON-NLS-1$

private static final DateTimeZone TIMEZONE_BERLIN = DateTimeZone
    .forID(TIMEZONE_BERLIN_ID);

private static final DateTimeZone TIMEZONE_MOSCOW = DateTimeZone
    .forID("Europe/Moscow"); //$NON-NLS-1$

@Test
public void testDateTimeConversion() {
    final Date sourceDateTime = new DateTime(2015, 3, 15, 12, 55).toDate();
    // We assume that sourceDateTime is in TIMEZONE_MOSCOW. We want to convert it to TIMEZONE_BERLIN.
    // Note that the conversion should work regardless whether sourceDateTime contains any timezone
    // information. It must be converted from TIMEZONE_MOSCOW to TIMEZONE_BERLIN (regardless of whether
    // a timezone is specified in it).
    final Date expectedResult =
        new DateTime(2015, 3, 15, 12 - 2, 55).toDate(); // At 12:55 in Moscow, it is 10:55 in Berlin.
    final Date actualResult =
        new DateTime(sourceDateTime, TIMEZONE_MOSCOW).toDateTime(
            TIMEZONE_BERLIN).toDate();
    Assert.assertEquals(expectedResult, actualResult);
}

为什么 actualResult 包含错误的日期时间?

new DateTime(sourceDateTime, TIMEZONE_MOSCOW).withZone(TIMEZONE_BERLIN).toDate() 也不行。

一个java.util.Date对象不包含时区信息。它只是自 01-01-1970 00:00:00 GMT 以来几毫秒的包装器。您不能拥有处于特定时区的 Date 对象。 Date 对象总是指代 "absolute" 时间点。您不应该假装 Date 对象包含特定时区(例如莫斯科或柏林时区)的日期和时间。

相反,您在格式化 Date 对象以供显示时指定时区,方法是设置要在 DateFormat 对象上看到 Date 的时区:

DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z");
df1.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));

DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z");
df2.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));

Date now = new Date();

// Note: The same Date object, but displayed in different timezones
System.out.println("The time in Berlin: " + df1.format(now));
System.out.println("The time in Moscow: " + df2.format(now));

这个似乎有效:

@Test
public void testDateTimeConversion() {
    final Date sourceDateTime = new DateTime(2015, 3, 15, 12, 55, TIMEZONE_MOSCOW).toDate();
    final Date expectedResult =
        new DateTime(2015, 3, 15, 12 - 2, 55, TIMEZONE_BERLIN).toLocalDateTime().toDate();

    final DateTime timeInMoscow =
        new DateTime(sourceDateTime, TIMEZONE_MOSCOW);
    final DateTime timeInBerlin = timeInMoscow.toDateTime(TIMEZONE_BERLIN);
    final Date actualResult = timeInBerlin.toLocalDateTime().toDate();

    Assert.assertEquals(expectedResult, actualResult);
}

我认为这解决了您的问题。只需将此逻辑转换为 JUnit 即可。

    DateTimeZone TIMEZONE_BERLIN = DateTimeZone.forID("Europe/Berlin");
    DateTimeZone TIMEZONE_MOSCOW = DateTimeZone.forID("Europe/Moscow");

    //moscow timezone 12:55
    DateTime sourceTimeMoscow = new DateTime(2015, 3, 15, 12, 55, TIMEZONE_MOSCOW);
    //berlin time expected 10:55
    Date expectedResult = new DateTime(2015, 3, 15, 10, 55, TIMEZONE_BERLIN).toLocalDateTime().toDate();
    //get actual result in Berlin in reference to Moscow
    Date actualResult = new DateTime(sourceTimeMoscow, TIMEZONE_BERLIN).toLocalDateTime().toDate();

    //for display purposes
    System.out.println(expectedResult); //expected result Berlin Time
    System.out.println(actualResult); //actual result Berlin Time

希望此解决方案能解决您的问题。