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

J13t0u picture J13t0u · Jun 11, 2011 · Viewed 245.4k times · Source

How to convert System.currentTimeMillis(); to seconds?

long start6=System.currentTimeMillis();
System.out.println(counter.countPrimes(100000000)+" for "+start6);

The console shows me 5761455 for 1307816001290. I can't read how many seconds that is.

Any help?

Answer

JH. picture JH. · Jun 12, 2011

TimeUnit

Use the TimeUnit enum built into Java 5 and later.

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