-1

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?

user70192
  • 13,786
  • 51
  • 160
  • 240
  • 2
    If you only care about **mm:ss.SSS** why are you using `nanoTime()`? Use [milliseconds](http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#currentTimeMillis%28%29), which has a built-in interface with `java.util.Date`. – blgt Jun 04 '14 at 15:05
  • @blgt: java.util.Date is a timestamp and has nothing to do with durations. – jarnbjo Jun 04 '14 at 15:07
  • @blgt because OP wants to measure the time that takes to run a piece of code. – Luiggi Mendoza Jun 04 '14 at 15:07
  • 1
    @jarnbjo So? Both are used to measure time, and 1milli == 10^6 nano. If he only wants 3 dec places after the decimal dot, millisecods are more than sufficient. The nano precision is lost anyway. – blgt Jun 04 '14 at 15:10
  • @blgt: What is your concept of "time". If you mean to measure a duration, 'java.util.Date' can't be used for that, it represent an instant of time (timestamp) and not a duration. – jarnbjo Jun 04 '14 at 15:13
  • 1
    @jarnbjo Technically, a timestamp is nothing more than the duration from the beginning of the epoch. The `Date` object measures this duration in *milliseconds*. Anyway, I was suggesting it as a human-readable format (which is what the question was about) – blgt Jun 04 '14 at 15:18
  • @blgt the standard way to measure time in Java is using `System.nanoTime()` since it is more accurate than milliseconds and using `new Date()`, explained in more details here: [How do I measure time elapsed in Java?](http://stackoverflow.com/q/1770010/1065197). – Luiggi Mendoza Jun 04 '14 at 15:21
  • Are you creating your own (micro)benchmark for your algorithms? – Luiggi Mendoza Jun 04 '14 at 15:21
  • 1
    @LuiggiMendoza Yes, it is more accurate. However, the full accuracy will be lost in the formatting the question asks for; for which milliseconds are sufficient. – blgt Jun 04 '14 at 15:25
  • 2
    @blgt: What you are actually writing is that if you start with a timestamp (beginning of the epoch) and add a duration, you get another timestamp. So far, that is correct. It does however not mean, that a duration and a timestamp are the same. – jarnbjo Jun 04 '14 at 15:41

1 Answers1

-2

Just convert it to milliseconds:

import java.util.concurrent.TimeUnit;
...
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.

Rob Eden
  • 362
  • 2
  • 7