如何将值从纳秒转换为秒?
这是代码段:
import java.io.*;
import java.util.concurrent.*;
..
class Stamper {
public static void main (String[] args) {
long start = System.nanoTime();
//some try with nested loops
long end = System.nanoTime();
long elapsedTime = end - start;
System.out.println("elapsed: " + elapsedTime + "nano seconds\n");
//convert to seconds
TimeUnit seconds = new TimeUnit();
System.out.println("which is " + seconds.toSeconds(elapsedTime) + " seconds");
}}
错误是
Stamper.java:16: enum types may not be instantiated.
这是什么意思?
TimeUtil
,因为它是enum
(枚举数)。如果要使用TimeUnit
,则应TimeUnit.NANOSECONDS.toSeconds(elapsedTime)
改用。祝好运!