Java.lang.System.nanoTime() 方法
描述
java.lang.System.nanoTime() 方法返回最精确的可用系统计时器的当前值,以纳秒为单位。 返回的值表示自某个固定但任意时间(将来,因此值可能为负)以来的纳秒,并提供纳秒精度,但不一定是纳秒精度。
声明
以下是 java.lang.System.nanoTime() 方法的声明。
public static long nanoTime()
参数
NA
返回值
该方法返回系统定时器的当前值,以纳秒为单位。
异常
NA
示例
下面的例子展示了 java.lang.System.nanoTime() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class SystemDemo {
public static void main(String[] args) {
// returns the current value of the system timer, in nanoseconds
System.out.print("time in nanoseconds = ");
System.out.println(System.nanoTime());
// returns the current value of the system timer, in milliseconds
System.out.print("time in milliseconds = ");
System.out.println(System.currentTimeMillis());
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
time in nanoseconds = 255073580723571 time in milliseconds = 1349311227921

