安卓得到当前UTC时间时间、UTC

2023-09-12 02:55:18 作者:ぐ╰╮初拥权者╭╯

什么是函数来获取当前UTC时间。我曾尝试与System.getCurrentTime但我得到设备的当前日期和时间。

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.

感谢

推荐答案

System.currentTimeMillis的()确实给你的毫秒数自1970年1月1日00:00 :00 UTC。你看到当地的时间可能是因为您转换日期实例为字符串,使用它之前的原因。您可以使用日期格式 s要转换日期 s到字符串 S在任何时区:

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());

还有see此相关的问题。