Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does System.currentTimeMillis() return UTC time?

I want to get the current UTC time in millis. I searched google and got some answers that System.currentTimeMillis() does returns UTC time. but it does not. If I do following:

long t1 = System.currentTimeMillis(); long t2 = new Date().getTime(); long t3 = Calendar.getInstance().getTimeInMillis(); 

all three times are almost same ( difference is in milli seconds due to calls ).

t1 = 1372060916 t2 = 1372060917 t3 = 1372060918 

and this time is not the UTC time instead this is my timezone time. How can i get the current UTC time in android?

like image 837
g.revolution Avatar asked Jun 24 '13 08:06

g.revolution


People also ask

What does currentTimeMillis return?

currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

How long is system currentTimeMillis?

System. currentTimeMillis() returns a Java long . It will always be exactly 64 bits long and will typically have a number of leading zeroes.

What is UTC time now in 24 hour format?

UTC time in ISO-8601 is 09:05:16Z. Note that the Z letter without a space.

Is UTC time in milliseconds?

There is no such things as "UTC milliseconds". A millisecond is an SI unit that is one thousandth of a second. ECMAScript Date instances hold a time value that is an offset in milliseconds from 1970-01-01T00:00:00Z (the ECMAScript epoch).


2 Answers

All three of the lines you've shown will give the number of milliseconds since the unix epoch, which is a fixed point in time, not affected by your local time zone.

You say "this time is not the UTC time" - I suspect you've actually diagnosed that incorrectly. I would suggest using epochconverter.com for this. For example, in your example:

1372060916 = Mon, 24 Jun 2013 08:01:56 GMT 

We don't know when you generated that value, but unless it was actually at 8:01am UTC, it's a problem with your system clock.

Neither System.currentTimeMillis nor the value within a Date itself are affected by time zone. However, you should be aware that Date.toString() does use the local time zone, which misleads many developers into thinking that a Date is inherently associated with a time zone - it's not, it's just an instant in time, without an associated time zone or even calendar system.

like image 137
Jon Skeet Avatar answered Oct 11 '22 22:10

Jon Skeet


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; } 
like image 37
jlhonora Avatar answered Oct 11 '22 22:10

jlhonora