Home  >  Article  >  LocalDate.parse and ResolverStyle.STRICT, for Asia/Hong Kong, return unexpected results

LocalDate.parse and ResolverStyle.STRICT, for Asia/Hong Kong, return unexpected results

王林
王林forward
2024-02-13 20:54:071147browse

php editor Xiaoxin will introduce the issues about LocalDate.parse and ResolverStyle.STRICT in Java in this article. When using these methods in Asia or Hong Kong, you may get unexpected results. We will explore this issue and provide solutions.

Question content

My input is 19.12.0009, the expected value is dec 19 00:00:00 hkt 9, but the returned result is dec 21 00:23:18 hkt 9, why? code show as below:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.uuuu")
                        .withResolverStyle(ResolverStyle.STRICT);
LocalDate localDate = LocalDate.parse("19.12.0009", formatter);
Instant instant = Instant.from(localDate.atStartOfDay(ZoneId.of("Asia/Hong_Kong")));
Date resultDate = Date.from(instant);
System.out.println("resultDate" + resultDate);

Solution

There is a concept of transition in zonerules, which has savingslocaltransitions. I found that for some reason (probably historical), the savings in "asia/hong_kong" local conversion start at 1904-10-30t00:36:42. resolverstyle.strict Not the problem here!

That's why you see 00:23:18 hkt 9.

If you change the code, for example:

localdate localdate = localdate.parse("19.12.1904", formatter);

Expected output:

resultDate Mon Dec 19 01:00:00 JST 1904

The above is the detailed content of LocalDate.parse and ResolverStyle.STRICT, for Asia/Hong Kong, return unexpected results. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete