[英]does System.currentTimeMillis() return UTC time?
I want to get the current UTC time in millis.我想以毫秒为单位获取当前的 UTC 时间。 I searched google and got some answers that System.currentTimeMillis() does returns UTC time.
我搜索了谷歌并得到了一些 System.currentTimeMillis() 确实返回 UTC 时间的答案。 but it does not.
但事实并非如此。 If I do following:
如果我执行以下操作:
long t1 = System.currentTimeMillis();
long t2 = new Date().getTime();
long t3 = Calendar.getInstance().getTimeInMillis();
all three times are almost same ( difference is in milli seconds due to calls ).所有三个时间几乎相同(由于调用而差异以毫秒为单位)。
t1 = 1372060916
t2 = 1372060917
t3 = 1372060918
and this time is not the UTC time instead this is my timezone time.这次不是UTC时间,而是我的时区时间。 How can i get the current UTC time in android?
如何在android中获取当前的UTC时间?
All three of the lines you've shown will give the number of milliseconds since the unix epoch, which is a fixed point in time, not affected by your local time zone.您显示的所有三行都将给出自 unix 纪元以来的毫秒数,这是一个固定的时间点,不受本地时区的影响。
You say "this time is not the UTC time" - I suspect you've actually diagnosed that incorrectly.您说“这次不是 UTC 时间”-我怀疑您实际上诊断错误了。 I would suggest using epochconverter.com for this.
我建议为此使用epochconverter.com 。 For example, in your example:
例如,在您的示例中:
1372060916 = Mon, 24 Jun 2013 08:01:56 GMT
We don't know when you generated that value, but unless it was actually at 8:01am UTC, it's a problem with your system clock.我们不知道您何时生成该值,但除非它实际上是在 8:01 am UTC,否则这是您的系统时钟的问题。
Neither System.currentTimeMillis
nor the value within a Date
itself are affected by time zone. System.currentTimeMillis
和Date
本身中的值都不受时区影响。 However, you should be aware that Date.toString()
does use the local time zone, which misleads many developers into thinking that a Date
is inherently associated with a time zone - it's not, it's just an instant in time, without an associated time zone or even calendar system.但是,您应该知道
Date.toString()
确实使用了本地时区,这会误导许多开发人员认为Date
与时区有内在关联——它不是,它只是一个瞬间,没有关联的时间区甚至日历系统。
I can confirm that all three calls could depend on the local time, considering the epoch, not the Date.toString()
or any similar method.我可以确认所有三个调用都可能取决于当地时间,考虑到时代,而不是
Date.toString()
或任何类似的方法。 I've seen them depend on local time in specific devices running Android 2.3.我已经看到它们依赖于运行 Android 2.3 的特定设备的本地时间。 I haven't tested them with other devices and android versions.
我还没有用其他设备和安卓版本测试过它们。 In this case, the local time was set manually.
在这种情况下,本地时间是手动设置的。
The only reliable way to get an independent UTC time is requesting a location update using the GPS_PROVIDER
.获得独立 UTC 时间的唯一可靠方法是使用
GPS_PROVIDER
请求位置更新。 The getTime()
value of a location retrieved from NETWORK_PROVIDER
also depends on local time.从
NETWORK_PROVIDER
检索到的位置的getTime()
值也取决于本地时间。 Another option is ping a server that returns a UTC timestamp, for example.例如,另一个选项是 ping 返回 UTC 时间戳的服务器。
So, what I do is the following:所以,我做的是以下内容:
public static String getUTCstring(Location location) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = sdf.format(new Date(location.getTime()));
// Append the string "UTC" to the date
if(!date.contains("UTC")) {
date += " UTC";
}
return date;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.