Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get operating system Time zone in Java?

Tags:

java

timezone

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.

like image 431
Evgenia Avatar asked Oct 17 '22 11:10

Evgenia


1 Answers

You can check system property user.timezone:

   System.getProperty("user.timezone")
like image 151
Evgeniy Dorofeev Avatar answered Oct 21 '22 09:10

Evgeniy Dorofeev