I use System.currentTimeMillis() to save the time a user starts an activity.
public class TimeStamp {
protected long _startTimeMillis = System.currentTimeMillis();
public String getStartTime() {
return new Time(_startTimeMillis).toString();
}
the class is instantiated when activity is started and getStartTime() returns the correct time. I also try to get the time that has passed after the activity has been started.
public String getElapsedTime() {
return new Time(System.currentTimeMillis() - _startTimeMillis).toString();
}
This works perfectly fine using an emulated android device (android 4.0.3). But when I deploy the application on my real android device (android 4.0.3) getElapsedTime() starts with one additional hour and then counts up normally. So on my real device getElapsedTime() will return "01:00:01" after the activity was started, but it should return "00:00:01".
Do you have any ideas why this happens?
You should use SystemClock.uptimeMillis()
.
uptimeMillis() is counted in milliseconds since the system was booted and is guaranteed to be monotonic whereas System.currentTimeMillis() isn't because the user or an application can change it at any time. Also, usually automatic time synchronization is enabled which can change the time at any time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With