我有以下功能
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");
}
测试通过,但不应该。从我的下划线开始,我应该收到一些差异一小时的结果。为什么我在所有这些测试中都有相同的时间?
答案 0 :(得分: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");
}
答案 1 :(得分:0)
检查DST转换日期的一种简单方法是从ZoneRules
获取ZoneId
对象:
ZoneRules rules = ZoneId.of("Brazil/East").getRules();
然后,您可以查看rules.getTransitions()
返回的列表,查看发生这些更改的所有日期,以及更改前后的偏移量。
某些区域没有转换列表,但却有转换规则,可以使用rules.getTransitionRules()
进行检查。