SensorEvent.timestamp日期转换问题

时间:2012-08-26 13:51:00

标签: android android-sensors

我正在尝试将日期中的事件时间戳转换为我的代码:

Calendar c = Calendar.getInstance();
c.setTimeInMillis(event.timestamp/1000000);//time in ms (timestamp is in ns)
System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(c.getTime()));

为什么我得到1970-01-02?

1 个答案:

答案 0 :(得分:4)

我假设你正在使用SensorEvent.timestamp。文档没有提到这是自上次启动以来的纳秒时间内的唤醒时间(与SystemClock.uptimeMillis()相当),而不是自Unix时代以来的时间。简而言之,您的设备似乎已经清醒了不到两天。

此外,Calendar.getTime()返回一个Date对象,并且有一个Date构造函数需要几毫秒才能缩短代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date(event.timestamp / 1000000)));
相关问题