gpt4 book ai didi

android - 移动设备上的时间计算

转载 作者:太空狗 更新时间:2023-10-29 16:43:47 25 4
gpt4 key购买 nike

我想计算设备上最后一个事件与当前事件之间的时间差。但用户始终可以通过设置系统时间手动更改时间,因此时差可能与实际时间不同。有没有正确计算时差的方法?

最佳答案

您可以在两个期间之间使用 System.nanoTime(),然后相减。与 System.currentTimeMillis() 在用户更改设备时间时发生变化不同,System.nanoTime() 不会受到影响。

Returns the current value of the most precise available system timer, in nanoseconds.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

关于android - 移动设备上的时间计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13467217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com