Java Android 获取当前 UTC 时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2818086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android get Current UTC time
提问by xger86x
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.
获取当前UTC时间的函数是什么。我已经尝试过 System.getCurrentTime,但我得到了设备的当前日期和时间。
Thanks
谢谢
采纳答案by Josef Pfleger
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 DateFormat
s to convert Date
s to String
s in any timezone:
System.currentTimeMillis()
确实给你自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数。您看到本地时间的原因可能是因为您Date
在使用实例之前将其转换为字符串。您可以使用DateFormat
s在任何时区将Date
s转换为String
s:
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());
Also see this related question.
另请参阅此相关问题。
回答by Someone Somewhere
see my answer here:
在这里看到我的答案:
How can I get the current date and time in UTC or GMT in Java?
如何在 Java 中以 UTC 或 GMT 格式获取当前日期和时间?
I've fully tested it by changing the timezones on the emulator
我已经通过更改模拟器上的时区对其进行了全面测试