1、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).
Returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
翻译一下就是:
返回当前时间,以毫秒为单位。注意,虽然返回值的时间单位是毫秒,但值的粒度取决于底层操作系统,可能更大。例如,许多操作系统以数十毫秒为单位度量时间。
有关“计算机时间”和协调世界时(UTC)之间可能出现的细微差异的讨论,请参阅Date类的描述。
返回:
当前时间与1970年1月1日午夜之间的差值,以毫秒为单位。
补充说明
1)、从源码中可以看到,这个方式是一个native方法,该值由底层提供。
2)、该方法可以用来计算当前日期,当前星期几等,与Date的换算非常方便,JDK提供了相关的接口来换算。
3)、通过该方法获取的值的依据是当前系统的日期和时间,可以在系统设置中进行设置和修改。
2、System.nanoTime 的定义
Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds.
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.
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 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 same instance of a Java virtual machine, is computed.
For example, to measure how long some code takes to execute:
long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = System.nanoTime() - startTime;
To compare two nanoTime values
long t0 = System.nanoTime();
...
long t1 = System.nanoTime();
one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.
Returns:
the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds
Since:1.5
翻译一下就是:
返回正在运行的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,因为存在数值溢出的可能性。
返回:
运行中的Java虚拟机的高分辨率时间源的当前值,以纳秒为单位
开始版本:1.5
补充说明
1)、从源码中可以看到,这个方式是一个native方法,该值由底层提供。
2)、该方法只是用来计算运行时间差的。
3)、该方法所基于的时间是随机的,但在同一个JVM中,不同的地方使用的原点时间是一样的。
对于使用t1-t0<0的判断,可以用如下的代码进行验证
public static void main(String[] args) throws Exception {
int init=(int)Math.pow(2,31);
int t0= init-20;
int t1= -init-3;
int t2 = init - 3;
if (t1 - t0 > 0 || t2 - t0 > 0) {
System.out.println("1---------"+(t1-t0));
}else{
System.out.println("2---------"+(t1-t0));
}
}
原理是 -7-7=1111 + 0111 = 0110 =6
3、当调整服务器时间的时候,currentTimeMillis是会受到影响的,有可能会导致后面获取的时间比前面的时间小 。
但是nanoTime 不是基于系统时间,同一个JVM中 原点时间 是一致的,所以调整服务器时间,后面时间还是比前面的时间大,但是需要采用B-A>0的方式来判断。