0

我有以下功能

public String getDateAndTimeInUserFormat(String value, DateTimeFormat inputFormat) {
    return ZonedDateTime.parse(value, DateTimeFormatter.ofPattern(inputFormat.getFormat()))
            .withZoneSameInstant(ZoneId.of(user().getTimezone()))
            .format(DateTimeFormatter.ofPattern(user().getDateFormat() + " " + user().getTimeFormat()));
}

和以下测试

@Test
public void getDateAndTimeInUserFormat() throws Exception {
    I18NService service = prepareForUserFormatCheck("####.000", "Brazil/Acre");
    assertEquals(service.getDateAndTimeInUserFormat("2017-05-06T20:52:52+0200"),"06-05-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-12-06T20:52:52+0200"),"06-12-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-01-06T20:52:52+0200"),"06-01-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-03-06T20:52:52+0200"),"06-03-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-07-06T20:52:52+0200"),"06-07-2017 13:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-09-06T20:52:52+0200"),"06-09-2017 13:52");
}

测试通过但不应该。根据我的理解,我应该收到一些相差一小时的结果。为什么我有相同的时间进行所有这些测试?

4

2 回答 2

1

似乎问题出在时区上。夏令时不适用于巴西/英亩。这是另一个证明它工作正常的测试。

  @Test
  public void getDateAndTimeInUserFormat() throws Exception {
    I18NService service = prepareForUserFormatCheck("####.000", "Brazil/East");
    assertEquals(service.getDateAndTimeInUserFormat("2017-01-06T20:52:52+0200"),"06-01-2017 16:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-02-06T20:52:52+0200"),"06-02-2017 16:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-03-06T20:52:52+0200"),"06-03-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-04-06T20:52:52+0200"),"06-04-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-05-06T20:52:52+0200"),"06-05-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-06-06T20:52:52+0200"),"06-06-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-07-06T20:52:52+0200"),"06-07-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-08-06T20:52:52+0200"),"06-08-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-09-06T20:52:52+0200"),"06-09-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-10-06T20:52:52+0200"),"06-10-2017 15:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-11-06T20:52:52+0200"),"06-11-2017 16:52");
    assertEquals(service.getDateAndTimeInUserFormat("2017-12-06T20:52:52+0200"),"06-12-2017 16:52");
  }
于 2018-02-22T15:04:03.897 回答
0

检查 DST 转换日期的一种简单方法是从以下位置获取ZoneRules对象ZoneId

ZoneRules rules = ZoneId.of("Brazil/East").getRules();

然后您可以查看由 , 返回的列表rules.getTransitions(),以查看发生这些更改的所有日期,以及更改前后的偏移量。

有些区域没有转换列表,而是有转换规则,可以使用rules.getTransitionRules().

于 2018-02-22T15:53:09.623 回答