Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. System timezone

System timezone

Scheduled Pinned Locked Moved Java
question
11 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hans Ruck
    wrote on last edited by
    #1

    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.

    L J 2 Replies Last reply
    0
    • H Hans Ruck

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • L Lost User

        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

        H Offline
        H Offline
        Hans Ruck
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • H Hans Ruck

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • H Hans Ruck

            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.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            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.

            B 1 Reply Last reply
            0
            • J jschell

              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.

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              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.

              J 1 Reply Last reply
              0
              • B Bernhard Hiller

                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.

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                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.

                B L 2 Replies Last reply
                0
                • J jschell

                  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.

                  B Offline
                  B Offline
                  Bernhard Hiller
                  wrote on last edited by
                  #8

                  Thanks for that information.

                  1 Reply Last reply
                  0
                  • J jschell

                    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.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    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

                    J 1 Reply Last reply
                    0
                    • L Lost User

                      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

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #10

                      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?

                      L 1 Reply Last reply
                      0
                      • J jschell

                        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?

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        jschell wrote:

                        My suggestion was that one needs to run tzutil from the java application via Process.

                        And it works as you described. :thumbsup:

                        Veni, vidi, abiit domum

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups