Home > Java > body text

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

王林
Release: 2024-02-13 20:54:07
forward
1174 people have browsed it

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);
Copy after login

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);
Copy after login

Expected output:

resultDate Mon Dec 19 01:00:00 JST 1904
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!