8 Replies Latest reply: Mar 28, 2007 11:54 AM by 807606 RSS

    Curious result using Date.getTime()

    807606
      I have found a frustrating problem with the getTime() method in the java.utils.Date class.
      Try this, fixing your operating system so that the first time you run the routine you are in DST and the next you are not.
      public class CheckingCurrentTime {
           public static void main(String[] args) {
                Date d = new Date();  // new date in host machine's timezone
                System.out.println("Date:: " + d);
                System.out.println("Date.getTime():: " + d.getTime());
           }
      }
      I have found that on Java 1.6 that this returns approximately the same time (within seconds which is the time to change to DST). So far so good. The docs say this time is GMT and so should not be altered by the DST. Great! Unfortunately there is a problem.
      Trying ' select from_unixtime([d.getTime()]/1000); ' on MySQL (v4) yields a time one hour ahead of GMT in both cases. What is going on? Is the MySQL function at fault - I can't find any reports that it is. Or am I miss reading the whole Date class!!!! I am running the above program in London UK during DST (but that should affect anything as the Date class gets its cue from the OS - right?)
      Can anyone please shed some light on this issue as I am completely stumped.

      Regards

      Steve Hodson
        • 1. Re: Curious result using Date.getTime()
          807606
          Actually, you are incorrect in assuming that Java's Date class gets its time from the OS.

          The JRE keeps its own record of time, regardless of what time is being kept by MySQL or your OS.

          Version 1.6 of the JRE should already be patched for the new DST rules however for an earlier version of Java you will need to update to the latest patch or run the tzupdater tool provided by Sun.
          • 2. Re: Curious result using Date.getTime()
            791266
            Actually, you are incorrect in assuming that Java's
            Date class gets its time from the OS.
            O_o

            Are you saying that current time isn't taken from the system clock?
            • 3. Re: Curious result using Date.getTime()
              807606
              Java gets the current time from the system clock, and the default TZ from the os's setting. Time is rebased so it starts at zero = January 1, 1970, 00:00:00 GMT, and the current value is the number of milliseconds since that time. Localized time is achieved by applying Java's timezone mappings to this current value. The mappings contains historical, current, and future (if known) values.

              The APIs for java.util.Date, Calendar, GregorianCalendar, and TimeZone have the details.

              If localized times are not required, then I guess that updated timezone mappings could be ignored.


              http://forum.java.sun.com/thread.jspa?threadID=5140155&tstart=135

              Although the above still doesn't answer my original query.
              • 4. Re: Curious result using Date.getTime()
                807606
                I am very sorry to everyone for wasting their time but I have discovered the solution. MySQL was built when the OS time was still in BST (oh dear...) so MySQL must have picked up this fact and used this when converting long time to a readable format. Solution simply involved rebuilding MySQL after setting the box to read GMT. So it wasn't the JRE.

                Many thanks to those who answered though.

                Steve
                • 5. Re: Curious result using Date.getTime()
                  807606
                  Actually, you are incorrect in assuming that
                  Java's
                  Date class gets its time from the OS.
                  O_o

                  Are you saying that current time isn't taken from the
                  system clock?
                  Yes essentially I am.

                  I have JRE 1.4.2_09 on my machine right now and when I run apps from this JRE the current time is displayed an hour earlier than my OS system time which is correct per changed DST rules.

                  My JRE would be correct if it were truly capturing the system time and displaying it in my apps but it isn't.

                  I have to run the tzupdater tool to fix this in my JRE.
                  • 6. Re: Curious result using Date.getTime()
                    807606
                    I think differentiation between the system clock and the operating system clock would be a good thing. The operating system clock (windows clock in this case) derives its time from the system clock (rtc - onboard). Java derives its time from the system clock(rtc - onboard) as well, making it independent of the operating system clock and the settings applied to it.
                    • 7. Re: Curious result using Date.getTime()
                      791266
                      Actually, you are incorrect in assuming that
                      Java's
                      Date class gets its time from the OS.
                      O_o

                      Are you saying that current time isn't taken from
                      the
                      system clock?
                      Yes essentially I am.

                      I have JRE 1.4.2_09 on my machine right now and when
                      I run apps from this JRE the current time is
                      displayed an hour earlier than my OS system time
                      which is correct per changed DST rules.
                      That doesn't have anything to do with the system clock. That is just a presentation issue. The JRE does not use an internal clock (read reply #3)
                      My JRE would be correct if it were truly capturing
                      the system time and displaying it in my apps but it
                      isn't.
                      No, because you are applying tz information to the time when you display it. You can display the time using any tz if you want to, the source for the time is still the system clock.

                      Kaj
                      • 8. Re: Curious result using Date.getTime()
                        807606
                        I apologize for the misunderstanding...

                        I assumed that "system clock" meant the OS clock!

                        My apologies to Kaj and the OP, I didn't mean to add confusion.