javadatetimeformatter

LocalDate.parse for Asia/Hong_Kong, return unexpected result


my input is 19.12.0009, which expect Dec 19 00:00:00 HKT 9, but the return is Dec 21 00:23:18 HKT 9, why? the code is as follow:

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

  • In ZoneRules there is concept of transitions, which inside have savingsLocalTransitions. I figured out that savings in "Asia/Hong_Kong" local transitions starts with 1904-10-30T00:36:42 for some reason (probably historic reasons). ResolverStyle.STRICT is no the issue here!

    This is the reason you see 00:23:18 HKT 9.

    If you change your code, for example like that:

    LocalDate localDate = LocalDate.parse("19.12.1904", formatter);
    

    output will be expected:

    resultDate Mon Dec 19 01:00:00 JST 1904