Reputation: 1330
In my application, I need to parse a JSON that includes the end time of an event. When this event is in progress, I need to calculate the time left for the finalization of that evet. To do this I use this piece of code:
// I get the time and convert it from millis to seconds
long end = System.currentTimeMillis() / 1000;
techsTime.add((int) (queue.event.get("event0").endtime - end));
Once I've the time in seconds, I convert it to d,h,m,s. This is working like a charm in some devices like the Optimus Black and the HTC Desire. But today I've tested my app with the ZTE Blade of a friend and the time left of the event is very strange. It appears in negative numbers and digits very distance from the original ones. I've tested the app with different values and there's no pattern, they're like random numbers, e.g:
Optimus Black -> 21m 32s
ZTE Blade -> -38m -57s
I've thought that System.currentTimeMillis() could be different in both devices, but both of them are using CM7 as OS.
Upvotes: 1
Views: 865
Reputation: 46943
I think the problem might be with the clock of one of the devices not set properly. With the time extracts you give it seems like the difference is exactly one hour, which might be a cause of wrong clock setting. When you consider the time set, also take a look at the time zone. It might be that both devices show the same time, but one of them is configured in different time zone.
It will be very weird for modern device not to sync its clock in the net, but still possible for ZTE for example.
Upvotes: 2
Reputation: 6517
Use System.nanotime()
- it is specially created in order to measure the time between 2 events.
System.currentTimeMillis()
uses the system clock which does not update every 1 millisecond and thus is unreliable and giving different results.
Upvotes: 1