那里!
我必须在不调用系统时钟或系统时区的情况下获得准确的当前 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时间戳吗?
您的代码示例最终都基于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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句