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.