oliverkn
oliverkn

Reputation: 743

Stopwatch resistant against system time changes

I want to make a stopwatch in Java, which is immune against system time changes.

long start = System.nanoTime();

// System time changes

long stop = System.nanoTime();

long elapsed = start - stop;

So the problem with this code is that when the system time is changing, then the time measurement is wrong.

Upvotes: 0

Views: 63

Answers (1)

nils
nils

Reputation: 1382

The above code should be resistant against system time changes according to the API:

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.

Upvotes: 3

Related Questions