This question already has an answer here:
I'm recording the duration of how long something takes in Java. To do that, I'm running:
long startTime = System.nanoTime();
long duration = System.nanoTime() - startTime;
Now, I would like to take the duration and print it out in a format of
mm:ss.SSS. How do I do that in Java?
Best How To :
Just convert it to milliseconds:
long duration_ms = TimeUnit.NANOSECONDS.toMilliseconds( duration )
Then DateFormat will work as expected.
Upon further reflection, my answer was incorrect. The problem is that System.currentTimeMillis() (and DateFormat) want milliseconds since epoch. The conversion method above will convert the duration to milliseconds so you could use it along with some math if you had an initial value in nanoseconds and ms since epoch, but System.nanoTime() is not defined to be relative to anything in particular.