Android get Current UTC time

JavaAndroidDatetimeTimeUtc

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGerardoView Question on Stackoverflow
Solution 1 - JavaJosef PflegerView Answer on Stackoverflow