82

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

K3NN3TH
  • 1,408
  • 2
  • 18
  • 31
Gerardo
  • 5,748
  • 11
  • 63
  • 93

1 Answers1

124

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.

Community
  • 1
  • 1
Josef Pfleger
  • 73,505
  • 15
  • 96
  • 99