Java Reference ยท java.lang
Java System.currentTimeMillis() and nanoTime()
Reference for System.currentTimeMillis() and System.nanoTime(): wall-clock versus monotonic time, measuring elapsed time, resolution, overflow and alternatives.
Java offers two basic clocks in System. currentTimeMillis() returns the wall-clock time as milliseconds since the Unix epoch (1 January 1970, UTC). nanoTime() returns an arbitrary, ever-increasing counter in nanoseconds that is only meaningful when you subtract two readings.
Use the first to know what time it is, and the second to measure how long something took.
long wall = System.currentTimeMillis(); long start = System.nanoTime(); // ... work ... long elapsedNanos = System.nanoTime() - start;
Try it
Real clock values change on every run, so these examples print only deterministic facts about them.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public static native long currentTimeMillis() public static native long nanoTime()
| Method | Returns |
|---|---|
currentTimeMillis() |
Milliseconds since 1970-01-01T00:00:00Z, from the system clock. |
nanoTime() |
Nanoseconds from an unspecified origin; the value may be negative. Only differences are meaningful. |
Usage notes
- Never print or store
nanoTime()values and never compare them to dates. The origin can change between JVM runs. - Subtract, do not compare. Use
end - start, notend > start, so the code stays correct even if the counter wraps around itslongrange (it takes about 292 years, but the subtraction rule is the documented one). currentTimeMillis()can jump. The operating system may adjust the clock for NTP corrections or the user changing the time. A measured "duration" can be negative or huge.nanoTime()is monotonic and is not affected.- Resolution is not accuracy.
nanoTime()has nanosecond units, but actual granularity depends on the hardware and OS, often tens of nanoseconds. - Microbenchmarks lie. JIT compilation, garbage collection and warm-up distort naive timing loops. Use JMH for serious measurements.
- Prefer
java.time.Instant.now()gives wall time with up to nanosecond precision (platform dependent) and, withClock, a testable source of time.Duration.between()does the arithmetic and prints readably. - Testing. Code that calls
System.currentTimeMillis()directly is hard to test; inject aClockinstead. - Cheap but not free. Both calls cost roughly 20 to 50 ns on modern systems; do not call them per element in tight loops.
- Leap seconds are not represented:
currentTimeMillis()ignores them.
Related
System,Thread,Math.round()- Lessons: Dates and times, Threads