【问题标题】:Stopwatch resistant against system time changes秒表可抵抗系统时间变化
【发布时间】:2014-04-28 20:44:55
【问题描述】:

我想用 Java 做一个秒表,它不受系统时间变化的影响。

long start = System.nanoTime();

// System time changes

long stop = System.nanoTime();

long elapsed = start - stop;

所以这段代码的问题是,当系统时间发生变化时,时间测量是错误的。

【问题讨论】:

  • 不应该。 System.nanoTime() 使用的源是一个对系统时间变化敏感的计数器。无论如何 --> Guava 有Stopwatch

标签: java date time stopwatch


【解决方案1】:

根据 API,上面的代码应该能够抵抗系统时间的变化:

此方法只能用于测量经过的时间,不能用于 与任何其他系统或挂钟时间概念相关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    相关资源
    最近更新 更多