Android get Current UTC time


Question

What is the function to get the current UTC time. I have tried with System.getCurrentTime but i get the current date and time of the device.

Thanks

1
67
4/2/2015 4:12:41 PM

Accepted Answer

System.currentTimeMillis() does give you the number of milliseconds since January 1, 1970 00:00:00 UTC. The reason you see local times might be because you convert a Date instance to a string before using it. You can use DateFormats to convert Dates to Strings in any timezone:

DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());

Also see this related question.

108
5/23/2017 12:18:20 PM

see my answer here:

How can I get the current date and time in UTC or GMT in Java?

I've fully tested it by changing the timezones on the emulator


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon