How to measure the a time-span in seconds using System.currentTimeMillis()? How to measure the a time-span in seconds using System.currentTimeMillis()? java java

How to measure the a time-span in seconds using System.currentTimeMillis()?


TimeUnit

Use the TimeUnit enum built into Java 5 and later.

long timeMillis = System.currentTimeMillis();long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);


long start = System.currentTimeMillis();counter.countPrimes(1000000);long end = System.currentTimeMillis();System.out.println("Took : " + ((end - start) / 1000));

UPDATE

An even more accurate solution would be:

final long start = System.nanoTime();counter.countPrimes(1000000);final long end = System.nanoTime();System.out.println("Took: " + ((end - start) / 1000000) + "ms");System.out.println("Took: " + (end - start)/ 1000000000 + " seconds");


like so:

(int)(milliseconds / 1000)