Android get Current UTC time [duplicate]
Asked Answered
S

1

83

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

Stonecrop answered 12/5, 2010 at 10:56 Comment(1)
Why not just use new Date(System.currentTimeMillis())?Potation
G
127

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.

Golightly answered 12/5, 2010 at 11:35 Comment(3)
I want to user UTC+2 , how can I do thtNubbin
new Date() will return the current system date! if user changes the time/date it will return wrong values!Prenomen
val c = Calendar.getInstance() c.timeZone = TimeZone.getTimeZone("UTC") c.add(Calendar.HOUR, 2)Disreputable

© 2022 - 2024 — McMap. All rights reserved.