先来看看官方说明吧
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
google 翻译:
> 返回正在运行的 Java 虚拟机的高分辨率时间源的当前值,以纳秒为单位。
> 此方法只能用于测量经过的时间,与系统或挂钟时间的任何其他概念无关。
> 返回的值表示自某个固定但任意的原始时间以来的纳秒(可能在将来,因此值可能为负数)。
> 在Java虚拟机实例中,此方法的所有调用都使用相同的来源;其他虚拟机实例可能使用不同的来源
> 此方法提供纳秒精度,但不一定提供纳秒分辨率(即值更改的频率) - 不保证分辨率至少与 currentTimeMillis() 的分辨率一样好。
> 由于数值溢出,超过大约 292 年(263 纳秒)的连续调用的差异将无法正确计算经过的时间。
> 仅当在 Java虚拟机的同一实例中获得的两个此类值之间的差异被计算时,此方法返回的值才有意义。
> 例如,要测量一些代码执行需要多长时间:
> long startTime = System.nanoTime();
> ... 正在测量的代码 ...
> long estimateTime = System.nanoTime() - startTime;
> 比较两个 nanoTime 值
> long t0 = System.nanoTime();
> ...
> long t1 = System.nanoTime();
> 应该使用 t1 - t0 < 0,而不是 t1 < t0,因为数值溢出的可能性。
> 返回:正在运行的 Java 虚拟机的高分辨率时间源的当前值,以纳秒为单位
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. See Also: java.util.Date
google 翻译:
> 以毫秒为单位返回当前时间。
> 请注意,虽然返回值的时间单位是毫秒,但值的粒度取决于底层操作系统,并且可能更大。
> 例如,许多操作系统以几十毫秒为单位测量时间。
> 有关“计算机时间”和协调世界时 (UTC) 之间可能出现的细微差异的讨论,请参阅类 Date 的描述。
> 返回: 当前时间与 UTC 1970 年 1 月 1
> 日午夜之间的差异,以毫秒为单位。
对比
-
nanoTime
正在运行的 Java 虚拟机
仅当在 Java虚拟机的同一实例中获得的两个此类值之间的差异被计算时,此方法返回的值才有意义
说明和你所运行环境的 JVM 有关,不同 JVM,等到的结果不同;
且两个值进行比较和计算时,也需要在同一实例中才有意义。只能用于测量经过的时间,与系统或挂钟时间
说明不能用于表示时间,但是可以进行时间间隔的精确测算,可以参考上述解释的例子。单位是纳秒
-
currentTimeMillis
单位是毫秒
值的粒度取决于底层操作系统,并且可能更大
说明存在一定的误差,这里比较 nanoTime 的话,在计算时间间隔或者比较大小时,nanoTime 会更精确。与 UTC 有关,受其影响
这里也侧面说明,实际上用于时间展示问题不大,但是要测量时间间隔,还是有一定影响的。
总结
如果对时间间隔的计算要求精度更高的时候,使用
System.nanoTime()
如果对时间间隔的计算精度要求不高,或者只是需要用来表示时间的时候,使用
System.currentTimeMillis()