I was about to use System.currentTimeMillis() in Eclipse when I noticed the following paragraph appear in its Javadoc:
Returns the current system time in milliseconds since January 1, 1970
00:00:00 UTC. This method shouldn't be used for measuring timeouts or
other elapsed time measurements, as changing the system time can affect
the results.
So that means I can’t rely on System.currentTimeMillis() if I wanted to specifically maintain a reference to this current point in time. What is the most accurate way to do it, then? Do the three methods mark the current time differently from each other?
UPDATE: What I’m trying to do is measure the difference between two points in time in two separate program runs. My fear is that if I use System.currentTimeMillis() and the user plays with the system time after the first run, I might get a bizarre unexpected value during the second.
If you want to refer to a specific point in time, you can definitely rely on
System.currentTimeInMillis(). The three methods you describe will mark the time the same way, the difference is just what kind of object it is wrapped in. Use the variant that you need in your program.If you want to measure the difference between two points in time during a program run, use
System.nanoTime()instead.To get the time difference between two points in time in two separate program runs you will have to rely on external time sources if you are afraid that the user might play around with the system clock. For example, you can take a look at
Java SNTP Client.