How to measure the a time-span in seconds using System.currentTimeMillis()?
JavaMillisecondsJava Problem Overview
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?
Java Solutions
Solution 1 - Java
TimeUnit
Use the TimeUnit
enum built into Java 5 and later.
long timeMillis = System.currentTimeMillis();
long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);
Solution 2 - Java
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");
Solution 3 - Java
like so:
(int)(milliseconds / 1000)
Solution 4 - Java
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.
Solution 5 - Java
Java 8 now provides the most concise method to get current Unix Timestamp:
Instant.now().getEpochSecond();
Solution 6 - Java
I have written the following code in my last assignment, it may help you:
// A method that converts the nano-seconds to Seconds-Minutes-Hours form
private static String formatTime(long nanoSeconds)
{
int hours, minutes, remainder, totalSecondsNoFraction;
double totalSeconds, seconds;
// Calculating hours, minutes and seconds
totalSeconds = (double) nanoSeconds / 1000000000.0;
String s = Double.toString(totalSeconds);
String [] arr = s.split("\\.");
totalSecondsNoFraction = Integer.parseInt(arr[0]);
hours = totalSecondsNoFraction / 3600;
remainder = totalSecondsNoFraction % 3600;
minutes = remainder / 60;
seconds = remainder % 60;
if(arr[1].contains("E")) seconds = Double.parseDouble("." + arr[1]);
else seconds += Double.parseDouble("." + arr[1]);
// Formatting the string that conatins hours, minutes and seconds
StringBuilder result = new StringBuilder(".");
String sep = "", nextSep = " and ";
if(seconds > 0)
{
result.insert(0, " seconds").insert(0, seconds);
sep = nextSep;
nextSep = ", ";
}
if(minutes > 0)
{
if(minutes > 1) result.insert(0, sep).insert(0, " minutes").insert(0, minutes);
else result.insert(0, sep).insert(0, " minute").insert(0, minutes);
sep = nextSep;
nextSep = ", ";
}
if(hours > 0)
{
if(hours > 1) result.insert(0, sep).insert(0, " hours").insert(0, hours);
else result.insert(0, sep).insert(0, " hour").insert(0, hours);
}
return result.toString();
}
Just convert nano-seconds to milli-seconds.
Solution 7 - Java
TimeUnit.SECONDS.convert(start6, TimeUnit.MILLISECONDS);
Solution 8 - Java
For conversion of milliseconds to seconds, since 1 second = 10³ milliseconds:
//here m will be in seconds
long m = System.currentTimeMillis()/1000;
//here m will be in minutes
long m = System.currentTimeMillis()/1000/60; //this will give in mins
Solution 9 - Java
// Convert millis to seconds. This can be simplified a bit,
// but I left it in this form for clarity.
long m = System.currentTimeMillis(); // that's our input
int s = Math.max(
.18 * (Math.toRadians(m)/Math.PI),
Math.pow( Math.E, Math.log(m)-Math.log(1000) )
);
System.out.println( "seconds: "+s );