将 UTC 转换为当前区域设置时间
我正在从 Web 服务下载一些 JSON 数据。在此 JSON 中,我有一些日期/时间值。一切都以 UTC 时间为准。 如何解析此日期字符串,以便结果 Date 对象位于当前区域设置中?
例如:服务器返回“2011-05-18 16:35:01”,我的设备现在应该显示“2011-05-18 18:35:01”(GMT +2)
我当前的代码:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
I am downloading some JSON data from a webservice. In this JSON I've got some Date/Time values. Everything in UTC.
How can I parse this date string so the result Date object is in the current locale?
For example: the Server returned "2011-05-18 16:35:01" and my device should now display "2011-05-18 18:35:01" (GMT +2)
My current code:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它有一个设置时区的方法:
全部完成!
It has a set timezone method:
all done!
所以你想告知 SimpleDateFormat UTC 时区:
要显示:
So you want to inform SimpleDateFormat of UTC time zone:
To display:
干杯!
Cheers!
java.time
我想贡献现代答案。大多数其他答案都是正确的,并且在 2011 年都是很好的答案。如今,
SimpleDateFormat
早已过时,而且它总是带来一些惊喜。今天我们有了更好的:java.time
也称为 JSR-310,现代 Java 日期和时间 API。这个答案适用于任何接受外部依赖项(直到java.time
出现在您的 Android 设备上)或已经在使用 Java 8 或更高版本的人。此代码片段的结果是所要求的:
如果可以的话请给出明确的时区
我已经给出了欧洲/苏黎世的明确时区。请替换您所需的当地时区。要依赖设备的时区设置,请使用
ZoneId.systemDefault()
。但是,请注意,这是脆弱的,因为它从 JVM 获取时区设置,并且该设置可能会被程序的其他部分或同一 JVM 中运行的其他程序更改。在 Android 上使用 java.time
我向您保证了外部依赖项。要在 Android 上使用上述内容,您需要 ThreeTenABP、ThreeTen Backport 的 Android 版本、JSR-310 到 Java 6 和 7 的向后移植。 这个问题:如何在 Android 项目中使用 ThreeTenABP。
java.time
I should like to contribute the modern answer. Most of the other answers are correct and were fine answers in 2011. Today
SimpleDateFormat
is long outdated, and it always came with some surprises. And today we have so much better:java.time
also known as JSR-310, the modern Java date and time API. This answer is for anyone who either accepts an external dependency (just untiljava.time
comes to your Android device) or is already using Java 8 or later.The result of this code snippet is what was asked for:
Give explicit time zone if you can
I have given an explicit time zone of Europe/Zurich. Please substitute your desired local time zone. To rely on your device’s time zone setting, use
ZoneId.systemDefault()
. However, be aware that this is fragile because it takes the time zone setting from the JVM, and that setting could be changed under our feet by other parts of your program or other programs running in the same JVM.Using java.time on Android
I promised you an external dependency. To use the above on Android you need the ThreeTenABP, the Android edition of ThreeTen Backport, the backport of JSR-310 to Java 6 and 7. How to go about it is nicely and thoroughly explained in this question: How to use ThreeTenABP in Android Project.
你的字符串 myUTC = "2016-02-03T06:41:33.000Z"
Your String myUTC = "2016-02-03T06:41:33.000Z"
Date 对象始终是自 UTC 纪元 0 以来的毫秒数的包装器。它从不代表当地时间。
这意味着您需要创建第二个 SimpleDateFormatter 来创建本地时间的显示字符串。
编辑:@Op。请注意,Android 上日期/时间格式化的首选类是 java.text.DateFormat
A Date object is always a wrapper around milliseconds since epoch 0 in UTC. It does never represent local time.
That means that you need to create a second SimpleDateFormatter that creates a display string that is in local time.
Edit: @Op. Note that the preffered class for date/time-formatting on Android is java.text.DateFormat
如果您有一个时间戳(长)作为输入(对于 UTC 时间),因为这是在 Android 上,您可以执行以下操作:
CalendarEx.kt
DateHelper.kt
用法示例:
If you had a timestamp (Long) as input instead (for the UTC time), since this is on Android, you can do something as such:
CalendarEx.kt
DateHelper.kt
Example usage: