【问题标题】:Android get Current UTC time [duplicate]Android获取当前UTC时间[重复]
【发布时间】:2011-02-18 13:44:06
【问题描述】:

获取当前UTC时间的函数是什么。我尝试过使用 System.getCurrentTime,但我得到了设备的当前日期和时间。

谢谢

【问题讨论】:

  • 为什么不直接使用new Date(System.currentTimeMillis())

标签: java android datetime time utc


【解决方案1】:

System.currentTimeMillis() 确实为您提供了自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数。您看到当地时间的原因可能是因为您在使用 Date 实例之前将其转换为字符串。您可以在任何时区使用DateFormats 将Dates 转换为Strings:

DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());

还有see this related question

【讨论】:

  • 我想使用 UTC+2 ,我该怎么做
  • new Date() 将返回当前系统日期!如果用户更改时间/日期,它将返回错误的值!
  • val c = Calendar.getInstance() c.timeZone = TimeZone.getTimeZone("UTC") c.add(Calendar.HOUR, 2)
猜你喜欢
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
  • 1970-01-01
  • 2012-07-24
  • 2019-07-28
  • 1970-01-01
相关资源
最近更新 更多