0

I am having a trouble getting a timestamp string from onSensorChanged method, than write it into a Log File as this example Magnitic Timestamp column, i`m studding java by creating a sensor app.

In this app i used Calendar and Date method to get other sensors timestamp but in this case i want to use only timestamp.

Result is: 1970/01/01_08:59:59:926 but i want to get nows date and time like this : 2013/11/01_14:17:02:673

I am using the following code to write the Log File:

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;
    }
};

I can't get it right. Any suggestions? Thank you.

Zak Chapman
  • 355
  • 3
  • 8
  • I think the problem lies in `magniLogFileTimestamp = (event.timestamp - System.nanoTime()) / 1000000L;`. If you subtract the time-stamp with current time, then you get the offset time, hence the date value near the epoch (1970/01/01). Is this the expected behavior? – Andrew T. Nov 01 '13 at 06:00
  • it give the correct date if i use the code like this : lightLogFileTimestamp = (new Date()).getTime() + (event.timestamp - System.nanoTime()) / 1000000L; ....... lightFile.print(String.valueOf(sensorTimestamp .format(lightLogFileTimestamp))); – Zak Chapman Nov 01 '13 at 06:44
  • It looks that have no solution, as explained [**_HERE_**](http://developer.android.com/reference/android/os/SystemClock.html) , the time since the system was booted. instead google said [**_HERE_SensorEvent**](http://developer.android.com/reference/android/hardware/SensorEvent.html#timestamp) The time in nanosecond at which the event _happened_: which seems to be wrong in my openion – Zak Chapman Nov 05 '13 at 01:34

2 Answers2

0

I think the problem lies in magniLogFileTimestamp = (event.timestamp - System.nanoTime()) / 1000000L;. If you subtract the time-stamp with current time, then you get the offset time which is considerably small, hence the date value near the epoch (1970/01/01).

Instead of subtracting it, just use the event.timestamp directly.

magniFile.print(sensorTimestamp.format(event.timestamp / 1000000));
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • Thank you for reply, in this case i got : 3083/03/30_00:48:12:000 which is not the current date and time, more suggestions please? – Zak Chapman Nov 01 '13 at 06:40
  • I forgot that the `event.timestamp` is in nanosecond, where the `SimpleDateFormat` accepts millisecond instead. Updated the answer. – Andrew T. Nov 01 '13 at 06:42
  • I see it`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 it`s 16:03, should i create a method for get UTC, sorry i`m newbie – Zak Chapman Nov 01 '13 at 07:03
  • Seems I have a wrong assumption regarding `event.timestamp`. While it is true it's nanoseconds, it is not based from system time. Instead, it is from the device uptime. Read [this](http://stackoverflow.com/questions/5500765/accelerometer-sensorevent-timestamp) post for more info. And there is another [issue](https://code.google.com/p/android/issues/detail?id=56561) with it. – Andrew T. Nov 01 '13 at 07:19
  • It looks that have no solution, as explained [**_HERE_**](http://developer.android.com/reference/android/os/SystemClock.html) , the time since the system was booted. instead google said [**_HERE_SensorEvent**](http://developer.android.com/reference/android/hardware/SensorEvent.html#timestamp) The time in nanosecond at which the event _happened_: which seems to be wrong in my openion – Zak Chapman Nov 05 '13 at 01:29
0

If you want to get epoch in milliseconds use:

System.currentTimeMillis();

If you want to get printable date, try:

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

Hope this helps.

basitj
  • 263
  • 3
  • 11
  • Thank you, but if you look tight i don`t want to use Date which can handle it correctly, I supposed to use the timestamp case only – Zak Chapman Nov 01 '13 at 06:41
  • It looks that have no solution, as explained [**_HERE_**](http://developer.android.com/reference/android/os/SystemClock.html) , the time since the system was booted. instead google said [**_HERE_SensorEvent**](http://developer.android.com/reference/android/hardware/SensorEvent.html#timestamp) The time in nanosecond at which the event _happened_: which seems to be wrong in my openion – Zak Chapman Nov 05 '13 at 01:37