JDK-8299675 : ZoneId.systemDefault() doesn't reflect the change of system default time-zone because of cache
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 11,17,19,20
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2023-01-04
  • Updated: 2023-01-09
  • Resolved: 2023-01-09
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
According to https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/ZoneId.html#systemDefault(), it should be true, that "If the system default time-zone is changed, then the result of this method will also change."

This buggy (?) behavior was probably introduced with the following optimization:
https://bugs.openjdk.org/browse/JDK-8074002

I think that either implementation or javadoc should be fixed.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run jshell, invoke ZoneId.systemDefault():
jshell> java.time.ZoneId.systemDefault()
=> Initial/Timezone

Do not close jshell!

2. In parallel, change system time zone:
$ sudo timedatectl set-timezone Other/Timezone

3. Go back to jshell, invoke ZoneId.systemDefault() again:
jshell> java.time.ZoneId.systemDefault()


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
=> Other/Timezone
ACTUAL -
=> Initial/Timezone


Comments
The spec of `ZoneId.systemDefault()` is quite clear that it returns the `ZoneId` instance based on `TimeZone.getDefault()` value, which is *not* necessarily the same as the underlying platform's default zone. Consider the case if the user specifies the default zone with the system property `-Duser.timezone=XXX`, or an application calls `TimeZone.setDefault()`, then modifying platform's default zone has no effect.
09-01-2023

"system default time-zone" refers to `TimeZone.setDefault()`. System == JDK here.
05-01-2023

The observations on Windows 10: JDK 11: Failed, ZoneId did not change. JDK 17: Failed. JDK 19: Failed. JDK 20ea+9: Failed.
05-01-2023