6

I have a simple Java Executor thread running. This just detects the time zone and displays.

The code is as follows :

ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    exec.scheduleAtFixedRate(new Runnable() {
      @Override
      public void run() {

            TimeZone.setDefault(null);
            //get Calendar instance
            Calendar now = Calendar.getInstance();

            //get current TimeZone using getTimeZone method of Calendar class
            TimeZone timeZone = now.getTimeZone();

            //display current TimeZone using getDisplayName() method of TimeZone class
            System.out.println("Current TimeZone is : " + timeZone.getDisplayName());
            System.out.println( System.getProperty( "user.timezone" ) );

      }
    }, 0, 10, TimeUnit.SECONDS);

The problem is, if I modify the time zone alone manually using the control panel.My time zone is not getting updated.

I have read a post explaining JVM time issue Java System.getProperty( "user.timezone" ) does not work

Now i need to detect system time zone change

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Sizen
  • 91
  • 4
  • I am wondering about the real problem you intend to solve. In which setup would you expect that the *time zone* is changing? – GhostCat Oct 19 '16 at 10:50
  • @GhostCat - I am running this app in a Windows platform.The base idea is to detect system time zone changes and perform certain operations.As this application starts the timezone is detected properly...When the application is still running and time zone is modified...It is not reflected...That is the problem to me. – Sizen Oct 19 '16 at 10:52
  • I got that part. What I am wondering is: why would expect the *time zone* to change? Is that like: person *traveling* and taking a mobile device into another country? – GhostCat Oct 19 '16 at 10:52
  • @GhostCat - your point is valid.The system runs in a hardware and travels between borders... – Sizen Oct 19 '16 at 10:54
  • You'll need to access the Windows facilities for this. Java only gives you the environment as it was when you started the JVM up. You might be able to do this with native code. – RealSkeptic Oct 19 '16 at 10:57
  • @RealSkeptic - How can i achieve that using native code....I am a new bee to these things... – Sizen Oct 19 '16 at 11:02
  • By the way, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Mar 15 '18 at 23:34

2 Answers2

2

Given the latest comments, I have a workaround for you.

The problem is more or less: the JVM process will not notice when the timezone changes, so asking the JVM will not give you the information you need. That is why people suggest you to write some native (C/C++) code, and to call that from within the JVM to query Windows internals.

That should definitely work, but pulling together all the elements for this ... in a robust/correct way will be difficult (even for people beyond "beginner" level).

Thus, some other idea: do not use "native" code to query the time zone. Instead, you could be calling some script to do that; you grep its output and update your settings.

You could do that with python, or maybe directly with powershell.

So, your Java code would use a ProcessBuilder to run some command; and then you simply read from the outputstream of that process to fetch the string with the current (new) timezone.

Probably not the most robust solution in the world; but at least: quick to implement for some first testing.

Theoretically you could also try to run a Java class to print the timezone; that would have the nice advantage of getting the real Java name for the new Timezone for free!

Of course, the other aspect to keep in mind: you have to make sure that all your code is aware that the timezone can change!

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

Executing the following code resets the time zone to the updated system value:

    System.setProperty("user.timezone", "");
    TimeZone.setDefault(null);
M. Justin
  • 14,487
  • 7
  • 91
  • 130
Sizen
  • 91
  • 4
  • **Nope, incorrect** according to my testing. A call to `TimeZone.setDefault(null)` does *not* change to match the host OS’ current zone. Instead, that call reverts to the JVM’s initial default. Using Java 9.0.4 on macOS Sierra. I think the behavior I experienced was the meaning intended by the third sentence in [the method’s documentation](https://docs.oracle.com/javase/9/docs/api/java/util/TimeZone.html#setDefault-java.util.TimeZone-): *This method doesn't change the value of the user.timezone property.* – Basil Bourque Mar 16 '18 at 00:29
  • @BasilBourque This works on jdk-9.0.4 on my Mac. Example output from one run (excerpt in Danish): `Current TimeZone is : Centraleuropæisk normaltid Europe/Berlin Current TimeZone is : Central-normaltid America/Chicago … Current TimeZone is : Ulan Bator-normaltid Asia/Ulaanbaatar Current TimeZone is : Centraleuropæisk normaltid Europe/Berlin ` – Ole V.V. Mar 28 '19 at 04:10
  • Confirmed that this does not work for me on Java 11 on macOS Catalina 10.15.3 as well. – M. Justin Mar 25 '20 at 14:24
  • @BasilBourque You're correct that calling just `TimeZone.setDefault(null)` by itself does not work. From my own testing on macOS, both commands (that and `System.setProperty("user.timezone", "")`) need to be called for this to work. – M. Justin Mar 25 '20 at 14:27
  • Note that this won't work with `java.time.Clock.systemDefaultZone()` and are reusing it throughout the runtime of the app, as the time zone is fixed when the clock is created — though any new clocks created by this method will use the updated time zone. So care would have to be take to handle time zone changes in that case. – M. Justin Mar 25 '20 at 14:41