我正在使用 TelephonyManger.getAllCellInfo 来收集有关附近小区的信息。我注意到 CellInfo 包含一个名为 的字段mTimestamp
,根据文档是:
自启动以来,此单元信息在 nanos 中的大约时间
现在,我想把这个时间转换成一个实际的时间戳,它会给我采样的具体日期。
是否这样做:return System.currentTimeMillis() - timestampFromBootInNano / 1000000L;
转换它的正确方法?
编号mTimestamp
以自设备启动以来的纳秒为单位。System.currentTimeMillis()
自 1970 年 1 月 1 日午夜起以毫秒为单位测量。
你可以:
减去mTimestamp
从SystemClock.elapsedRealtimeNanos()
,以确定有多少纳秒前时间戳表示
将其转换为毫秒以确定时间戳代表多少毫秒前
减去它System.currentTimeMillis()
以确定制作时间戳的时间
所以,这给了我们:
long millisecondsSinceEvent = (SystemClock.elapsedRealtimeNanos() - timestampFromBootInNano) / 1000000L;
long timeOfEvent = System.currentTimeMillis() - millisecondsSinceEvent;
timeOfEvent
现在可以与诸如java.util.Calendar
ThreeTenABP 之类的东西一起使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句