How can I get the operating system Time Zone in Java?

TimeZone.getDefault () returns the system time zone before it was changed.

Example 1:

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

      

Result:

Europe/Kaliningrad

      

This is the system time zone.

Example 2:

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

      

Result:

Asia/Kolkata

      

This is not the system time zone, the system time zone is still in Europe / Kaliningrad.

So how can I get the system timezone even after changing the default DateTimeZone.

+3


source to share


2 answers


You can check the system property user.timezone:



   System.getProperty("user.timezone")

      

+1


source


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

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

      



and use this variable later.

0


source







All Articles