How to measure the a time-span in seconds using System.currentTimeMillis()?
like so:
(int)(milliseconds / 1000)
TimeUnit
Use the TimeUnit
enum built into Java 5 and later.
long timeMillis = System.currentTimeMillis();
long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);
From your code it would appear that you are trying to measure how long a computation took (as opposed to trying to figure out what the current time is).
In that case, you need to call currentTimeMillis
before and after the computation, take the difference, and divide the result by 1000 to convert milliseconds to seconds.
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");