使用 Android 获取 GMT 时间
我在 StackOverflow 上研究这个问题有一段时间了 Android 获取当前 UTC 时间 和 如何我在 Java 中获取当前的 UTC 或 GMT 日期和时间?
我尝试了两种方法来获取手机的当前 GMT 时间。我在西班牙,时差是 GMT+2。让我们看一个例子: 1° 尝试:我创建了一种格式并将其应用于 System.currentTimeMillis();
DateFormat dfgmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtTime = dfgmt.format(new Date());
//Using System.currentTimeMillis() is the same as new Date()
Date dPhoneTime = dfgmt.parse(gmtTime);
Long phoneTimeUTC = dPhoneTime.getTime();
我需要将这个时间减去另一个时间,这就是我将演员转换为 Long 的原因。
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date arrivalDate = df.parse(item.getArrivalDate());
//the String comes from JSON and is for example:"UTC_arrival":"2011-05-16 18:00:00"
//which already is in UTC format. So the DateFormat doesnt have the GMT paramater as dfgmt
diff = arrival.getTime() - phoneTimeUTC ;
我也尝试过这个:
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()
但我仍然没有得到正确的区别。但如果我这样做:
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()-3600000*2;
它确实可以正常工作。
有什么想法吗?
非常感谢,
大卫。
I have been digging into the question for a while in StackOverflow
Android get Current UTC time
and
How can I get the current date and time in UTC or GMT in Java?
I have tried two ways to get the current time of my phone in GMT. I am in Spain and the difference is GMT+2. So let's see with an example:
1º attemp: I created a format and applied it to System.currentTimeMillis();
DateFormat dfgmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtTime = dfgmt.format(new Date());
//Using System.currentTimeMillis() is the same as new Date()
Date dPhoneTime = dfgmt.parse(gmtTime);
Long phoneTimeUTC = dPhoneTime.getTime();
I need to substract that time to another time, that's why i do the cast to Long.
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date arrivalDate = df.parse(item.getArrivalDate());
//the String comes from JSON and is for example:"UTC_arrival":"2011-05-16 18:00:00"
//which already is in UTC format. So the DateFormat doesnt have the GMT paramater as dfgmt
diff = arrival.getTime() - phoneTimeUTC ;
I also tried this:
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()
And still I dont get the right difference. But if I do this:
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()-3600000*2;
It does work OK.
Any ideas?
Thanks a lot,
David.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这肯定有效!
指定格式,您将在 GMT 中得到它!
This works for sure!
Specify the format, and you will get it in GMT!
据我阅读 calendar.getTimeInMillis();返回以毫秒为单位的 UTC 时间。我使用了以下代码,并将其与本网站 http://www.xav.com/time 中的纪元进行了比较.cgi。
乔拉
As far as I read the calendar.getTimeInMillis(); returns the UTC time in millis. I used the following code and compared it to the Epoch in this site http://www.xav.com/time.cgi.
Giora
看看是否有效。
Have a look and see if that works.
您始终可以使用:
或
you can always use:
or
工作正常。
Works fine.
java.time 和 ThreeTenABP
我提供现代答案。
要获取现在(在西班牙或任何其他地方)的电话时间与过去某个时间之间的毫秒差异:
示例输出:
如果您想要以秒或其他时间单位为单位的差异,只需使用
ChronoUnit
枚举中的适当枚举常量而不是ChronoUnit.MILLIS
即可。无需担心设备时区,也无需担心格式化或解析时间,这些担心只会导致这个基本简单的事情变得过于复杂。
顺便说一句,纪元是一个明确定义的时间点,它不随时区而变化,在世界各地都是相同的。因此,从纪元到现在的毫秒数在所有时区也是相同的。有人说此计数始终采用 UTC 格式,因为纪元(通常)是用 UTC 定义的,如 1970 年 1 月 1 日 00:00 UTC。
问题:java.time 不需要 Android API 级别 26 吗?
java.time 在旧版和新版 Android 设备上都能很好地工作。它只需要至少 Java 6。
链接
java.time< /code> 首次被描述。
java.time
向后移植到 Java 6 和7(JSR-310 为 ThreeTen)。java.time and ThreeTenABP
I am providing the modern answer.
To get the difference in milliseconds between the phone time now — in Spain or any other place — and a certain time in the past:
Example output:
If you want the difference in seconds or some other time unit, just use the appropriate enum constant from the
ChronoUnit
enum instead ofChronoUnit.MILLIS
.There is no need to worry about the device time zone, nor about formatting or parsing the time, those worries only lead to over-complication of this basically simple matter.
BTW the epoch is one well-defined point in time, it doesn’t vary with time zone, it’s the same all over the world. Therefore the count of milliseconds from the epoch till now is also the same in all time zones. Some say that this count is always in UTC because the epoch is (usually) defined in UTC, as January 1, 1970 at 00:00 UTC.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.Links
java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).