6

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.

Evgenia
  • 335
  • 2
  • 12
  • the answer in previous post https://stackoverflow.com/questions/24806183/get-date-in-current-timezone-in-java – Ori Marko Jun 14 '17 at 12:00
  • 1
    @user7294900, it returns Asia/Calcutta instead Europe/Kaliningrad – Evgenia Jun 14 '17 at 12:07
  • What are you expecting? When you call `setDefault`, it changes the default timezone for the whole JVM, **that's the expected behaviour**. If you don't want to change it, then don't call `setDefault` (or save the timezone in a variable before changing the default, as already suggested in the answers below). –  Jun 14 '17 at 12:21
  • 1
    @Hugo, I want to get System Time Zone (which is set for the operating system) regardless of which time zone is set on JVM. – Evgenia Jun 14 '17 at 12:29
  • 1
    Maybe this can help: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/time-zone.html –  Jun 14 '17 at 12:32

2 Answers2

3

You can check system property user.timezone:

   System.getProperty("user.timezone")
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

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.

Bishwash
  • 854
  • 1
  • 9
  • 22