gpt4 book ai didi

java - ZonedDateTime 的夏令时未按预期工作

转载 作者:行者123 更新时间:2023-12-02 11:36:02 27 4
gpt4 key购买 nike

我有以下功能

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

关于java - ZonedDateTime 的夏令时未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48930198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com