Android get Current UTC time [duplicate]

試著忘記壹切 提交于 2019-12-17 07:11:11

问题


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:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2818086/android-get-current-utc-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!