【问题标题】:how to Get epoch timestamp by using event.timestamp only in android如何仅在android中使用event.timestamp获取纪元时间戳
【发布时间】:2013-11-12 07:58:45
【问题描述】:

我无法从 onSensorChanged 方法获取时间戳字符串,而不是将其写入日志文件作为示例 Magnitic Timestamp 列,我正在通过创建传感器应用程序来研究 Java。

在这个应用程序中,我使用日历和日期方法来获取其他传感器的时间戳,但在这种情况下,我只想使用时间戳。

结果是:1970/01/01_08:59:59:926 但我想像这样得到现在的日期和时间:2013/11/01_14:17:02:673

我正在使用以下代码编写日志文件:

final SensorEventListener magniListener = new SensorEventListener() {
    private float magnidT;
    private long magniLogFileTimestamp;

    @Override
    public void onSensorChanged(SensorEvent event) {
        magniLogFileTimestamp = (event.timestamp - System.nanoTime()) / 1000000L;
        if (magnitimestamp != 0) {
            magnidT = (event.timestamp - magnitimestamp);
            magniValueX = event.values[0];
            magniValueY = event.values[1];
            magniValueZ = event.values[2];
            //here write the sensor values and timestamp to CSV file
            if (magniFile != null) {
                magniFile.print(String.valueOf(sensorTimestamp
                        .format(magniLogFileTimestamp)));
                //sensorTimestamp = new SimpleDateFormat("yyyy/MM/dd_HH:mm:ss:SSS");
                magniFile.print("," + String.valueOf(magniValueX) + ","
                        + String.valueOf(magniValueY) + ","
                        + String.valueOf(magniValueZ));
                magniFile.println();
            }
        }
        magnitimestamp = event.timestamp;
        magniX.setText("X-axis: " + String.valueOf(magniValueX));
        magniY.setText("Y-axis: " + String.valueOf(magniValueY));
        magniZ.setText("Z-axis: " + String.valueOf(magniValueZ));
        magnDelayValue.setText("Magnitic Delay: "
                + String.valueOf(magnidT * NS2MS) + " ms");
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        magnitimestamp = 0;
        magniLogFileTimestamp = 0;
    }
};

我做错了。有什么建议?谢谢。

【问题讨论】:

  • 我认为问题出在magniLogFileTimestamp = (event.timestamp - System.nanoTime()) / 1000000L;。如果你用当前时间减去时间戳,那么你会得到偏移时间,因此是纪元 (1970/01/01) 附近的日期值。这是预期的行为吗?
  • 如果我使用这样的代码,它会给出正确的日期:lightLogFileTimestamp = (new Date()).getTime() + (event.timestamp - System.nanoTime()) / 1000000L; ....... lightFile.print(String.valueOf(sensorTimestamp .format(lightLogFileTimestamp)));
  • 看起来没有解决方案,正如HERE 解释的那样,系统启动以来的时间。相反,谷歌说HERE_SensorEvent事件_发生的纳秒时间:这在我的开放中似乎是错误的

标签: android timestamp sensors unix-timestamp logfile


【解决方案1】:

如果您想以毫秒为单位获取纪元,请使用:

System.currentTimeMillis();

如果您想获得可打印的日期,请尝试:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String datevalue = dateFormat.format(date);

希望这会有所帮助。

【讨论】:

  • 谢谢,但如果你看起来很紧张我不想使用可以正确处理它的日期,我应该只使用时间戳大小写
  • 看起来没有解决方案,正如HERE 解释的那样,系统启动以来的时间。相反,谷歌说HERE_SensorEvent事件_发生的纳秒时间:这在我的开放中似乎是错误的
【解决方案2】:

我认为问题在于magniLogFileTimestamp = (event.timestamp - System.nanoTime()) / 1000000L;。如果你用当前时间减去时间戳,那么你得到的偏移时间非常小,因此是纪元(1970/01/01)附近的日期值。

不用减去它,直接使用event.timestamp

magniFile.print(sensorTimestamp.format(event.timestamp / 1000000));

【讨论】:

  • 感谢您的回复,在这种情况下,我得到的是:3083/03/30_00:48:12:000 这不是当前的日期和时间,请提供更多建议?
  • 我忘记了 event.timestamp 以纳秒为单位,而 SimpleDateFormat 接受毫秒。更新了答案。
  • 我看到了s right, but i still get not correct date : 1970/01/01_19:10:54:633, my current date time is near in tokyo now its 16:03,我应该创建一个获取 UTC 的方法吗,对不起我是新手
  • 似乎我对event.timestamp 有一个错误的假设。虽然它确实是纳秒,但它不是基于系统时间。相反,它来自设备正常运行时间。阅读this 帖子了解更多信息。还有另一个issue
  • 看起来没有解决方案,正如HERE 解释的那样,系统启动以来的时间。相反,谷歌说HERE_SensorEvent事件_发生的纳秒时间:这在我的开放中似乎是错误的
猜你喜欢
  • 2022-12-16
  • 2011-03-24
  • 1970-01-01
  • 2020-08-29
  • 2018-03-15
  • 1970-01-01
  • 2012-11-16
  • 2021-05-10
  • 2021-07-20
相关资源
最近更新 更多