System.currentTimeMillis(),new Date().getTime()和Calendar.getInstance().getTimeInMillis()的准确性?

Mat*_*ros 1 java calendar date

System.currentTimeMillis()当我注意到它的Javadoc中出现以下段落时,我正准备在Eclipse中使用:

Returns the current system time in milliseconds since January 1, 1970
00:00:00 UTC. This method shouldn't be used for measuring timeouts or
other elapsed time measurements, as changing the system time can affect
the results.
Run Code Online (Sandbox Code Playgroud)

所以这意味着System.currentTimeMillis()如果我想专门维护对当前这个时间点的引用,我就不能依赖它.那么,最准确的方法是什么?这三种方法是否标记当前时间彼此不同?

更新:我要做的是在两个单独的程序运行中测量两个时间点之间的差异.我担心的是,如果我使用System.currentTimeMillis()并且用户在第一次运行后使用系统时间,我可能会在第二次运行期间获得奇怪的意外值.

dac*_*cwe 6

您应该使用System.nanoTime()API文档:

返回最精确的可用系统计时器的当前值,以纳秒为单位.

此方法只能用于测量经过的时间,与系统或挂钟时间的任何其他概念无关.返回的值表示纳秒,因为某些固定但是任意时间(可能在将来,因此值可能为负).该方法提供纳秒精度,但不一定是纳秒精度.不保证值的变化频率.由于数值溢出,跨越大于约292年(2 63纳秒)的连续调用的差异将无法准确计算经过的时间.