Evgenia
Evgenia

Reputation: 363

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.

Upvotes: 8

Views: 8860

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136022

You can check system property user.timezone:

   System.getProperty("user.timezone")

Upvotes: 3

Bishwash
Bishwash

Reputation: 884

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.

Upvotes: 0

Related Questions