I can confirm that all three calls could depend on the local time, considering the epoch, not the Date.toString()
or any similar method. I've seen them depend on local time in specific devices running Android 2.3. I haven't tested them with other devices and android versions. In this case, the local time was set manually.
The only reliable way to get an independent UTC time is requesting a location update using the GPS_PROVIDER
. The getTime()
value of a location retrieved from NETWORK_PROVIDER
also depends on local time. Another option is ping a server that returns a UTC timestamp, for example.
So, what I do is the following:
public static String getUTCstring(Location location) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = sdf.format(new Date(location.getTime()));
// Append the string "UTC" to the date
if(!date.contains("UTC")) {
date += " UTC";
}
return date;
}