如何将 CellInfo 时间戳转换为实际日期?
How to convert CellInfo timestamp to actual date?
我正在使用 TelephonyManger.getAllCellInfo 收集附近小区的信息。我注意到 CellInfo 包含一个名为 mTimestamp
的字段,根据文档,它是:
Approximate time of this cell information in nanos since boot
现在,我想将这个时间转换为实际的时间戳,这将告诉我采样的具体日期。
这样做是否:return System.currentTimeMillis() - timestampFromBootInNano / 1000000L;
正确的转换方式?
没有。 mTimestamp
自设备启动后以纳秒为单位测量。 System.currentTimeMillis()
是从 1970 年 1 月 1 日午夜开始计算的毫秒数。
您可以:
从SystemClock.elapsedRealtimeNanos()
中减去mTimestamp
,以确定时间戳表示多少纳秒之前
将其转换为毫秒以确定时间戳表示多少毫秒之前
从 System.currentTimeMillis()
中减去它以确定时间戳的创建时间
所以,这给了我们:
long millisecondsSinceEvent = (SystemClock.elapsedRealtimeNanos() - timestampFromBootInNano) / 1000000L;
long timeOfEvent = System.currentTimeMillis() - millisecondsSinceEvent;
timeOfEvent
现在可以与 java.util.Calendar
、ThreeTenABP 等一起使用
我正在使用 TelephonyManger.getAllCellInfo 收集附近小区的信息。我注意到 CellInfo 包含一个名为 mTimestamp
的字段,根据文档,它是:
Approximate time of this cell information in nanos since boot
现在,我想将这个时间转换为实际的时间戳,这将告诉我采样的具体日期。
这样做是否:return System.currentTimeMillis() - timestampFromBootInNano / 1000000L;
正确的转换方式?
没有。 mTimestamp
自设备启动后以纳秒为单位测量。 System.currentTimeMillis()
是从 1970 年 1 月 1 日午夜开始计算的毫秒数。
您可以:
从
SystemClock.elapsedRealtimeNanos()
中减去mTimestamp
,以确定时间戳表示多少纳秒之前将其转换为毫秒以确定时间戳表示多少毫秒之前
从
System.currentTimeMillis()
中减去它以确定时间戳的创建时间
所以,这给了我们:
long millisecondsSinceEvent = (SystemClock.elapsedRealtimeNanos() - timestampFromBootInNano) / 1000000L;
long timeOfEvent = System.currentTimeMillis() - millisecondsSinceEvent;
timeOfEvent
现在可以与 java.util.Calendar
、ThreeTenABP 等一起使用