-
This method provides nanosecond precision, but not necessarily
-
nanosecond resolution (that is, how frequently the value changes)
-
- no guarantees are made except that the resolution is at least as
-
good as that of {@link #currentTimeMillis()}.
-
Differences in successive calls that span greater than
-
approximately 292 years (263 nanoseconds) will not
-
correctly compute elapsed time due to numerical overflow.
-
The values returned by this method become meaningful only when
-
the difference between two such values, obtained within the sam
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
e
-
instance of a Java virtual machine, is computed.
-
For example, to measure how long some code takes to execute:
-
{@code
-
long startTime = System.nanoTime();
-
// … the code being measured …
-
long estimatedTime = System.nanoTime() - startTime;}
-
To compare two nanoTime values
-
{@code
-
long t0 = System.nanoTime();
-
…
-
long t1 = System.nanoTime();}
-
one should use {@code t1 - t0 < 0}, not {@code t1 < t0},
-
because of the possibility of numerical overflow.
-
@return the current value of the running Java Virtual Machine’s
-
high-resolution time source, in nanoseconds
-
@since 1.5
*/
public static native long nanoTime();
翻译下来如下:
返回正在运行的Java虚拟机的高分辨率时间源的当前值,以纳秒计。
该方法可能仅仅用于测量已经逝去的时间,并且与任何其它系统或者挂钟时间概念无关。该返回值表示从某个固定但任意的原点时间(可能在未来,所以值可能是负数)开始的纳秒数。在一个java虚拟机实例中,所有该方法的调用都使用相同的原点;其它虚拟机实例很可能使用不同的源头。
该方法提供了纳秒级别的精度,但是不一定是纳秒级分辨率(也就是该值改变的频率)———— 除非这个分辨率至少和currentTimeMillis()一样好,否则将不会做任何保证。
在跨越大于292年(2的63次方纳秒)左右的连续调用中,这个差值将不能正确地计算已经过去的时间,因为数字溢出。
仅仅只有当在同一java虚拟机实例中获取的两个值之间的差值被计算时,返回值才有意义。
例如,去测量某代码执行花费了多长时间:
long startTime = System.nanoTime();
//…被测量的代码…
long estimatedTime = System.nanoTime() - startTime;
要比较两个nanoTime的值:
long t0 = System.nanoTime();
…
long t1 = System.nanoTime()。
因为数字溢出的可能性,您应该使用"t1 - t0 < 0",而不是"t1 < t0"(来判断它们的大小,笔者注)。
@return 当前正在运行的java虚拟机的高精度时间资源值,以纳秒为单位。
@since 1.5
System.currentTimeMillis()
/**
-
Returns the current time in milliseconds. Note that
-
while the unit of time of the return value is a millisecond,
-
the granularity of the value depends on the underlying
-
operating system and may be larger. For example, many
-
operating systems measure time in units of tens of
-
milliseconds.
-
See the description of the class
Date
for -
a discussion of slight discrepancies that may arise between
-
“computer time” and coordinated universal time (UTC).
-
@return the difference, measured in milliseconds, between
-
the current time and midnight, January 1, 1970 UTC.
-
@see java.util.Date
*/
public static native long currentTimeMillis();
翻译
返回当前时间(以毫秒为单位)。请注意,虽然返回值的时间单位是毫秒,但是*值的粒度
取决于基础操作系统,并且可能更大。例如,许多操作系统以数十*毫秒为单位测量时间。
-
有关*“计算机时间”和协调世界时(UTC)之间可能出现的细微差异的讨论,请参见类
-
Date </ code>的描述。
2,解读
currentTimeMillis()
(1)从源码中可以看到,这个方式是一个native方法,该值由底层提供。
(2)该方法可以用来计算当前日期,当前星期几等,与Date的换算非常方便,JDK提供了相关的接口来换算。