简体   繁体   中英

How can I get operating system Time zone in Java?

The TimeZone.getDefault() returns the System Time zone until it's changed.

Sample 1:

System.out.println(TimeZone.getDefault());

Result:

Europe/Kaliningrad

It is system time zone.

Sample 2:

TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(TimeZone.getDefault());

Result:

Asia/Kolkata

It isn't system time zone, system time zone is still Europe/Kaliningrad.

So how can I get system time zone even after change default DateTimeZone.

您可以检查系统属性user.timezone:

   System.getProperty("user.timezone")

Store the value of TimeZone.getDefault() in a variable before following codes

TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(TimeZone.getDefault());

and use the variable later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM