那里!
我必须得到准确的当前UTC时间,而不调用系统时钟或系统时区。我需要通过Time4j库获得精确的UTC时间。
问题是,在通过不同的方式获得UTC时间后,准确度与确切的UTC时间(特别是以秒为单位)不匹配。这对我来说很关键。
我需要得到精确的UTC时间戳,因为在我的机器上不匹配超过5秒和当前的UTC时间。我必须在他的tern更新DB中将这个时间戳传输到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时间戳?
发布于 2018-08-20 11:37:53
您的代码示例最终都是基于System.currentTimeMillis()的。如果您使用这个简单的时钟观察到的精度很低,那么您可以选择两种方法:
示例:
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 accesshttps://stackoverflow.com/questions/51917845
复制相似问题