In Java, System.nanoTime() returns the current value of the JVM's high-resolution timer in nanoseconds (billionths of a second). The number alone has no meaning: its starting point is arbitrary and it may even be negative.
It becomes useful when you subtract two readings: System.nanoTime() - start is the time that passed between them. Unlike the wall clock, this timer is not changed when the computer's date and time are adjusted, so it is the right tool for timing code, timeouts, and simple benchmarks.
A static method of the java.lang.System class, added in Java 5 (JDK 1.5). Call it on the class name, as in System.nanoTime(...). No import is needed, because classes in java.lang are always visible.
Syntax
Read the timer before and after the work, then subtract:
Java
long start = System.nanoTime();
doWork();
long elapsedNanos = System.nanoTime() - start;
long elapsedMs = TimeUnit.NANOSECONDS.toMillis(elapsedNanos);
Call
What it does
System.nanoTime()
Current timer value in nanoseconds; only differences are meaningful.
System.nanoTime() - start
Elapsed nanoseconds since start was read.
TimeUnit.NANOSECONDS.toMillis(n)
Converts nanoseconds to whole milliseconds (rounds down).
System.nanoTime() - deadline >= 0
Overflow-safe "has the deadline passed?" test.
"Nanosecond precision" is not "nanosecond accuracy": the timer may tick in larger steps, depending on the operating system and hardware. Differences only make sense between readings taken in the same running JVM.
Library declarations
Java
// java.lang.System
public static native long nanoTime()
Parameters
Parameter
Description
None
nanoTime() takes no parameters.
Return Value
Return Value
Description
long
The current timer value in nanoseconds, relative to a fixed but arbitrary origin that may be in the future (so the value can be negative).
Real date and time in ms since 1970, but it can jump when the clock is adjusted.
Instant.now()
A timestamp object for logs and dates, not meant for timing.
Duration.ofNanos(n)
Wraps an elapsed nanosecond count for readable output such as PT1.5S.
Thread.sleep(ms)
Waits at least about that long; measure the real pause with nanoTime().
Java Examples
Example 1 — Measure How Long a Loop Takes
The classic stopwatch pattern. The sum is fixed, but the time changes on every run, so only a check about it is printed.
Java
public class SystemNanoTimeMeasure {
public static void main(String[] args) {
long start = System.nanoTime();
long sum = 0;
for (int i = 1; i <= 1_000_000; i++) {
sum += i;
}
long elapsedNanos = System.nanoTime() - start;
System.out.println("Sum: " + sum);
System.out.println("Elapsed is not negative: " + (elapsedNanos >= 0));
}
}
Output
Sum: 500000500000
Elapsed is not negative: true
How It Works
Start the stopwatch. The first nanoTime() reading is stored in start; its own value is meaningless and is never printed.
A fixed result. Adding 1 to 1000000 gives 1000000 * 1000001 / 2 = 500000500000, which needs a long because it is larger than the biggest int.
Stop and subtract. The second reading minus start is the loop's running time in nanoseconds. It differs per run and per computer, so the program prints only that it is not negative.
Example 2 — Convert Elapsed Nanoseconds with TimeUnit
Nanoseconds are hard to read. TimeUnit converts them; the fixed values show how the conversion rounds.
Measured the pause: true
2500000000 ns = 2500 ms
2500000000 ns = 2 s
1500000 ns = 1.5 ms
How It Works
Time a pause.Thread.sleep(15) waits roughly 15 ms; the measured value depends on the operating system's timer, so only a check about it is printed.
TimeUnit cuts off the rest. 2500000000 ns is 2500 ms exactly, but in whole seconds toSeconds rounds down and returns 2, not 2.5.
Keep decimals with a double. Dividing by 1000000.0 (a double) instead of using TimeUnit keeps the fraction, so 1500000 ns prints as 1.5 ms.
Example 3 — Wait Until a Deadline the Safe Way
Timeouts compute a deadline once and compare with subtraction, as the documentation recommends.
Java
import java.util.concurrent.TimeUnit;
public class SystemNanoTimeDeadline {
public static void main(String[] args) {
long timeout = TimeUnit.MILLISECONDS.toNanos(30);
long deadline = System.nanoTime() + timeout;
while (System.nanoTime() - deadline < 0) {
// keep checking, for example whether a task has finished
}
System.out.println("Timeout in ns: " + timeout);
System.out.println("Deadline passed: " + (System.nanoTime() - deadline >= 0));
}
}
Output
Timeout in ns: 30000000
Deadline passed: true
How It Works
Milliseconds to nanoseconds.toNanos(30) multiplies by 1000000, so the timeout is 30000000 ns, a fixed number that is safe to print.
Subtract, then compare with 0.nanoTime() - deadline < 0 means "not yet". Writing nanoTime() < deadline could give the wrong answer if the values overflow, which subtraction avoids.
Guaranteed after the loop. The loop only ends once the difference is 0 or more, and the timer does not go backwards, so the final check prints true.
Frequently Asked Questions
The current value of a high-resolution timer in nanoseconds. The origin is arbitrary, so the value is only useful when you subtract an earlier reading from a later one.
currentTimeMillis is the wall-clock time since 1970 and can jump when the clock changes. nanoTime is a timer for elapsed time that is not affected by clock changes, but it cannot tell you the date.
Yes. The starting point is arbitrary and may be in the future, so the raw value can be negative. Differences between two readings are still correct.
Subtract the start value first, then use TimeUnit.NANOSECONDS.toMillis(elapsed), or divide by 1000000.0 to keep decimals.
No. It has nanosecond precision (the unit), but the real resolution depends on the hardware and operating system, often tens of nanoseconds or more.