2015-05-14 46 views
0

我正在使用(也许不正确)乔达日期时间在提醒应用程序中存储提醒时间。当设备上的时区更改(由于DST或刚移动到不同的TZ)时,我希望能够重置提醒以使用当前时区,但保留时间。更改Joda DateTime时区但不更改时间

例如:我在5月15日星期五的15:00设置提醒。如果时区在此时间之前发生变化,我希望能够在当地时间5月15日星期五15:00重新创建提醒。

我已经尝试使用withZoneRetainFields,但我一直没能保持时间保持不变:

new DateTime(reminderTimeMillis, DateTimeZone.forID(previousTimeZone)).withZoneRetainFields(DateTimeZone.getDefault()) 

回答

0

嗯,这方法应该主要工作:

@Test 
public void change_timezone_of_reminder() { 
    DateTime originalReminderDateTime = LocalDateTime.parse("2015-05-15T15:00").toDateTime(
      DateTimeZone.forID("Europe/Berlin")); 
    assertThat(originalReminderDateTime.toString(), equalTo("2015-05-15T15:00:00.000+02:00")); 
    long reminderMillis = originalReminderDateTime.getMillis(); 
    DateTime updatedTime = new DateTime(reminderMillis, DateTimeZone.forID("Europe/Berlin")) 
      .withZoneRetainFields(DateTimeZone.forID("America/New_York")); 
    assertThat(updatedTime.toLocalDateTime(), equalTo(LocalDateTime.parse("2015-05-15T15:00"))); 
    assertThat(updatedTime.toString(), equalTo("2015-05-15T15:00:00.000-04:00")); 
} 

那么,如何你评估时间字段没有改变?另外,如果您将提醒时间存储为新纪元后,则如果您使用全时区(“欧洲/柏林”)计算毫秒,则无需为DST班次调整它而不是一个固定的抵消。