Android 获取当前 UTC 时间 [英] Android get Current UTC time

查看:29
本文介绍了Android 获取当前 UTC 时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取当前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 以来的毫秒数.您看到本地时间的原因可能是因为您在使用 Date 实例之前将其转换为字符串.您可以使用 DateFormats 在任何时区将 Dates 转换为 Strings:

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

还有 查看此相关问题.

这篇关于Android 获取当前 UTC 时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆