1 2 Previous Next 22 Replies Latest reply: Apr 22, 2014 3:33 AM by gimbal2 RSS

    Problem with System.currentTimeMillis()

    user9012483

      I noticed with the java version "1.7.0.06" the function call to System.currentTimeMillis() is a second ahead of the date of the system.

       

       

      In a thread is always active(Ex: Servlet) everyday the difference between the system date and the date retrieved by the function System.currentTimeMillis() from the JVM increases by one second.

       

       

      Has anyone encountered the same problem?

       

      Andrea

        • 1. Re: Problem with System.currentTimeMillis()
          jwenting

          first could be just a rounding error.

          Second is nonsense if the first is true.

          • 2. Re: Problem with System.currentTimeMillis()
            user9012483

            I'm sorry maybe I was not clear enough.

            I try to explain it in another way.

             

             

             

            I have an older code that has always worked good.

            Last November we have migrated our platform and we have passed from Java 6 to Java 7.

             

             

            In the log file the date is written according to this function:

             

             

             

            private String Todate(String pattern, long value) {

             

             

              TimeZone TZ = TimeZone.getTimeZone(defaultTZ);

              TimeZone.setDefault(TZ);

              Calendar calendar = new GregorianCalendar();

             

             

              Date D = new Date(value);

             

             

              calendar.setTime(D);

              SimpleDateFormat formatter = new SimpleDateFormat(pattern);

             

             

              return formatter.format(calendar.getTime());

            }

             

             

             

             

             

            private static final String defaultTZ = "Europe/Rome";

            private static final String patternDate = "dd-MM-yyyy;HH:mm:ss";

             

             

             

            String todate = Todate(patternDate, System.currentTimeMillis());

             

             

             

             

             

            At this time the transactions outlined in the logs have a date with about 4 minutes longer than the system date:

            Log Date: 14-04-2014  14:19:26

            System Date: Mon Apr 14 14:15:02 METDST 2014

             

             

             

             

             

            The same code with java6 not showed problem.

            • 3. Re: Problem with System.currentTimeMillis()
              rp0428

              At this time the transactions outlined in the logs have a date with about 4 minutes longer than the system date:

              Log Date: 14-04-2014  14:19:26

              System Date: Mon Apr 14 14:15:02 METDST 2014

              What am I missing?

               

              I don't see ANYTHING that shows the system date being retrieved or being printed.

               

              Post code and result data that shows this:

              the function call to System.currentTimeMillis() is a second ahead of the date of the system.

              • 4. Re: Problem with System.currentTimeMillis()
                jwenting

                my guess: he's comparing the timestamp returned by System.currentTimeMillis() on one machine with a timestamp returned by some process on another machine and the clocks of the two happen to be a bit off.

                • 5. Re: Problem with System.currentTimeMillis()
                  user9012483

                  I opened two windows on my server "HP-UX B.11.23 U ia64 2328209431 unlimited-user license".

                   

                   

                  On the first one I run the comand "tail -f mylogfile.log"

                  On the other I am prepared to run the comand "date"

                   

                   

                  At the moment was traced a new operation I run the commands "date" on the other window.

                   

                   

                  I know this is not an orthodox methodology, but considering the big difference between dates, it is still effective.

                   

                   

                   

                  Then I did this test on my test server.

                  I wrote a simple class that prints the date every 24 hours using the fuction written earlier.

                   

                   

                   

                  import java.text.SimpleDateFormat;

                  import java.util.Calendar;

                  import java.util.Date;

                  import java.util.GregorianCalendar;

                  import java.util.TimeZone;

                   

                   

                  public class CancolaTempoTest {

                    public CancolaTempoTest() {

                    }

                   

                   

                    public static void main(String[] args) throws Exception {

                    try {

                    while(true){

                    System.out.println(Todate(patternDate, System.currentTimeMillis()));

                    Thread.sleep(86400000);

                    }

                    } catch (Exception e) {

                    e.printStackTrace();

                    } finally {

                    }

                    }

                   

                   

                    private static String Todate(String pattern, long value) {

                   

                   

                    TimeZone TZ = TimeZone.getTimeZone(defaultTZ);

                    TimeZone.setDefault(TZ);

                    Calendar calendar = new GregorianCalendar();

                   

                   

                    Date D = new Date(value);

                   

                   

                    calendar.setTime(D);

                    SimpleDateFormat formatter = new SimpleDateFormat(pattern);

                   

                   

                    return formatter.format(calendar.getTime());

                    }

                   

                    private static final String defaultTZ = "Europe/Rome";

                    private static final String patternDate = "dd-MM-yyyy;HH:mm:ss";

                  }

                   

                   

                  I run the program in background: "nohup java CancolaTempoTest &"

                  In this way, the software will print the date in the file nohup.out

                  The printed date will always be the same, while the date of the file's writing changes every day.

                  Every day it's a second less than the printed date.

                   

                   

                   

                  You can not believe it, but I'm not an idiot.

                  • 6. Re: Problem with System.currentTimeMillis()
                    jwenting

                    so you don't run the two at the same time, yet you wonder that the reported times are not the same? Sheesh...

                    Of course they're not the same, or do you seriously believe that you did all that within the same few milliseconds?

                     

                    It's not only "not orthodox", it's utterly wrong.

                    • 7. Re: Problem with System.currentTimeMillis()
                      user9012483

                      We're talking about a difference of 4 minutes .... not milliseconds.

                      • 8. Re: Problem with System.currentTimeMillis()
                        gimbal2

                        You know I don't want to put even more fuel on the fire, but I can't take threads that want to hint at a problem with the JVM seriously when said people run a very much outdated version. I'm pretty sure that the problem will persist even when you upgrade because if there is a problem its going to be in the OS (the source of whatever system time information the JVM receives), but until you try with the latest version there is just too much of a "what if...".

                        • 9. Re: Problem with System.currentTimeMillis()
                          user9012483

                          I agree with you. Although the version in question "Version 7.0.06 – May 2013 (includes Oracle update 7u21)" is not so dated.

                          Now I download and install the most recent update of Java7 for my OS "Version 7.0.09 – February 2014 (includes Oracle update 7u51)".

                           

                           

                          I hope to solve. Of course the problem is serious and sneaky.

                          • 10. Re: Problem with System.currentTimeMillis()
                            ++sja

                            You keep alternating between a one second difference (quite possibly a rounding issue; try logging milliseconds too) and a four-minute difference. Which is it?

                             

                            > On the first one I run the comand "tail -f mylogfile.log"

                             

                            If you want to test like this, you need to flush System.out in the Java program. Otherwise buffering may delay when the line is flushed to mylogfile.log. Also tail(1), depending on its implementation, might not notice new data immediately. Classic implementations of tail(1) loop once a second, trying to read their input - thus the output will lag an average of half a second behind its input - combine with millisecond rounding issue and that's your problem right there.

                             

                            Also if you test once a day, the various processes involved (java, terminal window, tail) may get swapped out, causing another small delay while they all get swapped back in.

                            • 11. Re: Problem with System.currentTimeMillis()
                              user9012483

                              I've seen that in a therad always active, the difference increases by one second per day.

                              The software in question has been running for several months ... so the difference between dates is about 4 minutes in growth.

                              In two months most likely it will be about 5 minutes. ( 1 seconds x 60 days)

                              • 12. Re: Problem with System.currentTimeMillis()
                                ++sja

                                Googling for "HP-UX time drift java" suggests that Java on HP-UX indeed may be doing something weird with the clock. See these:

                                http://h30499.www3.hp.com/t5/Languages-and-Scripting/JVM-Clock-Drift-date-time-not-in-synch-across-multiple-JVMs/td-p/3722555

                                http://www.experts-exchange.com/Programming/Languages/Java/Q_23308523.html

                                 

                                Try running with "java -XX:+UseGetTimeOfDay" to make Java use the system clock in the normal way, or see if the patches mentioned in above links apply to your OS version.

                                • 13. Re: Problem with System.currentTimeMillis()
                                  gimbal2

                                  This is about Java 7, not about an ancient version of Java (1.3), probably not even the Sun/Oracle JVM too, noted in an article from 2006.

                                  • 14. Re: Problem with System.currentTimeMillis()
                                    jwenting

                                    well, OP is running on HP so is using an HP provided JVM.

                                    Might well be it's a plausible answer, if HP JVMs still work that way and for whatever reason it's lagging a bit.

                                    There's a reference to some patch notes later in that thread on the hp forums, but those links are of course long dead (and EE banned my account when they started charging, guess they didn't like to give me grandfather status despite my grey hair).

                                    1 2 Previous Next