System timezone
-
Hi, Is there any way to read the current time zone of the operating system? I need to check it to see if it has been updated. I have to do this every minute, without restarting the application and without launching an external process. Thank you very much for your attention.
-
Hi, Is there any way to read the current time zone of the operating system? I need to check it to see if it has been updated. I have to do this every minute, without restarting the application and without launching an external process. Thank you very much for your attention.
-
Try the following:
TimeZone tz = Calendar.getInstance().getTimeZone();
System.out.println(tz.getDisplayName());
System.out.println(tz.getID());Note that an application can set its own time zone, independently of the system.
Veni, vidi, abiit domum
Thank you Richard. But try this and you will see what I mean:
TimeZone tz = Calendar.getInstance().getTimeZone();
System.out.println(tz.getDisplayName());
System.out.println(tz.getID());tz = Calendar.getInstance().getTimeZone(); // breakpoint here, stop, change the system time zone and then continue running
System.out.println(tz.getDisplayName());
System.out.println(tz.getID());The time zone is set at the beginning of the current JVM process. It is subsequently detached from the system time zone and it sadly seems that there is no orthodox way to reattach it...
Bogdan Rechi.
-
Thank you Richard. But try this and you will see what I mean:
TimeZone tz = Calendar.getInstance().getTimeZone();
System.out.println(tz.getDisplayName());
System.out.println(tz.getID());tz = Calendar.getInstance().getTimeZone(); // breakpoint here, stop, change the system time zone and then continue running
System.out.println(tz.getDisplayName());
System.out.println(tz.getID());The time zone is set at the beginning of the current JVM process. It is subsequently detached from the system time zone and it sadly seems that there is no orthodox way to reattach it...
Bogdan Rechi.
The timezone setting is part of the environment of every process and is set at the time the process is started. I cannot see any way to tell when it changes apart from some system process that can hook into the operating system in some way. Sorry.
Veni, vidi, abiit domum
-
Hi, Is there any way to read the current time zone of the operating system? I need to check it to see if it has been updated. I have to do this every minute, without restarting the application and without launching an external process. Thank you very much for your attention.
-
From the other responses.... Windows has the following command line utility tzutil.exe It has an option '/g' which returns the current timezone. You use java.lang.Process to run it and collect the results.
Just note that a process inherits the environment from its parent process - and when the time zone is part of the environment, then tzutil will report the time zone of the application it was started from, which may be different from the time zone of the system.
-
Just note that a process inherits the environment from its parent process - and when the time zone is part of the environment, then tzutil will report the time zone of the application it was started from, which may be different from the time zone of the system.
Bernhard Hiller wrote:
Just note that a process inherits the environment from its parent process
I just tried the following - Open a console window - Run tzutil to report current TZ - Leave console window running. - Change the TZ using the tray applet - In the same console window above use tzutil again - It reports the new TZ. The documentation also states that tzutil updates the TZ in the registry. Which wouldn't be an environment update. So that suggests that in terms of reporting it would report from the same source and not from the environment.
-
Bernhard Hiller wrote:
Just note that a process inherits the environment from its parent process
I just tried the following - Open a console window - Run tzutil to report current TZ - Leave console window running. - Change the TZ using the tray applet - In the same console window above use tzutil again - It reports the new TZ. The documentation also states that tzutil updates the TZ in the registry. Which wouldn't be an environment update. So that suggests that in terms of reporting it would report from the same source and not from the environment.
Thanks for that information.
-
Bernhard Hiller wrote:
Just note that a process inherits the environment from its parent process
I just tried the following - Open a console window - Run tzutil to report current TZ - Leave console window running. - Change the TZ using the tray applet - In the same console window above use tzutil again - It reports the new TZ. The documentation also states that tzutil updates the TZ in the registry. Which wouldn't be an environment update. So that suggests that in terms of reporting it would report from the same source and not from the environment.
-
Unfortunately that only works for a new execution of a program, e.g. tzutil. I just tried it with a running Java application, and the app did not see the change of timezone.
Veni, vidi, abiit domum
-
Not exactly sure what you mean. My suggestion was that one needs to run tzutil from the java application via Process. One would need to run it each time one wanted an update. You tried that and it didn't work?