1

那里!

我必须在不调用系统时钟或系统时区的情况下获得准确的当前 UTC 时间。我需要通过 Time4j 库获得精确的 UTC 时间。

通过不同方式获取 UTC 时间后,尤其是与确切的 UTC 时间不匹配的问题 - 以秒为单位的不匹配。这对我来说至关重要。

我需要获得准确的 UTC 时间戳,因为我的机器和当前 UTC 时间不匹配超过 5 秒。我必须将此时间戳传输到他的 tern 更新数据库中的 API 和 API。我每秒执行一些操作以及由于时间不够准确而无法执行的实际情况。

这里有一些我测试过的例子来获取当前的 UTC 时间戳:

Moment moment = Moment.UNIX_EPOCH;

First:
PlainTimestamp test1 = SystemClock.inZonalView("UTC").now();

Second:
Moment test2 = Moment.nowInSystemTime();

Third:
PlainTimestamp test3 = moment.toLocalTimestamp();

Forth:
PlainTimestamp test4 = Moment.nowInSystemTime().toZonalTimestamp("UTC");

我没有通过这些方法获得所需的准确性。

有什么方法可以通过 time4j 以非常高的精度获得实际的 UTC 时间戳,最高可达秒?

4

1 回答 1

1

您的代码示例最终都基于System.currentTimeMillis(). 如果您使用这个简单的时钟观察到低精度,那么您可以在两种方式之间进行选择:

  • 按照 JB Nizet 的建议,定期将您的操作系统与 NTP 服务器同步

  • 或者您可以使用包含替代时钟的包net.time4j.clock 。

例子:

  SntpConnector clock = new SntpConnector("ptbtime1.ptb.de"); // ntp-server-address
  clock.connect(); // requires internet connectivity, performs the synchronization
  Moment utc = clock.currentTime()); // can be called without actual internet access
于 2018-08-20T11:37:53.457 回答