java-如何使用System.currentTimeMillis()以秒为单位测量时间跨度?
怎么把5761455 for 1307816001290转换成秒?
long start6=System.currentTimeMillis();
System.out.println(counter.countPrimes(100000000)+" for "+start6);
控制台显示5761455 for 1307816001290。我看不懂那是多少秒。
有什么帮助吗?
J13t0u asked 2020-06-23T16:55:38Z
9个解决方案
75 votes
TimeUnit
使用Java 5和更高版本中内置的TimeUnit枚举。
long timeMillis = System.currentTimeMillis();
long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(timeMillis);
JH. answered 2020-06-23T16:56:20Z
63 votes
long start = System.currentTimeMillis();
counter.countPrimes(1000000);
long end = System.currentTimeMillis();
System.out.println("Took : " + ((end - start) / 1000));
更新
甚至更准确的解决方案是:
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");
Nico Huysamen answered 2020-06-23T16:55:56Z
15 votes
像这样:
(int)(milliseconds / 1000)
Stas Jaro answered 2020-06-23T16:56:39Z
8 votes
从您的代码看来,您正在尝试测量计算所花费的时间(而不是试图计算当前时间)。
在这种情况下,您需要在计算前后调用currentTimeMillis,进行求和,然后将结果除以1000,以将毫秒转换为秒。
NPE answered 2020-06-23T16:57:05Z
4 votes
Java 8现在提供了最简洁的方法来获取当前的Unix时间戳:
Instant.now().getEpochSecond();
Scadge answered 2020-06-23T16:57:24Z
3 votes
我在上一次作业中编写了以下代码,它可能对您有帮助:
// 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();
}
只需将纳秒转换为毫秒。
Eng.Fouad answered 2020-06-23T16:57:50Z
2 votes
TimeUnit.SECONDS.convert(start6, TimeUnit.MILLISECONDS);
pramodc84 answered 2020-06-23T16:58:05Z
2 votes
将毫秒转换为秒,因为1秒=10³毫秒:
//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
Manvendra_0611 answered 2020-06-23T16:58:25Z
1 votes
// 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 );
user1717831 answered 2020-06-23T16:58:41Z