Java 分区DateTime的夏令时未按预期工作

Java 分区DateTime的夏令时未按预期工作,java,datetime,dst,java-time,zoneddatetime,Java,Datetime,Dst,Java Time,Zoneddatetime,我有以下功能 public String getDateAndTimeInUserFormat(String value, DateTimeFormat inputFormat) { return ZonedDateTime.parse(value, DateTimeFormatter.ofPattern(inputFormat.getFormat())) .withZoneSameInstant(ZoneId.of(user().getTimezone()))

我有以下功能

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");
}

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

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

  @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");
  }

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

ZoneRules rules = ZoneId.of("Brazil/East").getRules();
然后,您可以检查由
rules.getTransitions()
返回的列表,查看发生这些更改的所有日期,以及更改前后的偏移量


有些区域没有转换列表,但有转换规则,可以使用
规则检查。getTransitionRules()

“它不应该”为什么?哪个断言是错误的?你试过调试它,看看这个怪物表达式的哪一部分是错误的吗?@M.Prokhorov看起来怪物表达式工作得很好。请核对答案。是的,我明白了。它看起来也不错(如果我们忽略这一点的话),这里是“巴西/英亩”自2014年以来全年使用相同的
UTC-5:00
补偿。事实上,在2013年11月10日之前,它使用的是UTC-4:00,也没有夏令时。