从JSON字符串确定TimeZone? [英] Determine TimeZone from a JSON String?
问题描述
我正在查询返回如下内容的JSON API:
I'm querying a JSON API that returns something like this:
{
"created_time": "2017-01-05T16:32:29+0100",
"updated_time": "2017-01-11T09:38:41+0100",
"id": "23842536731240607"
}
我需要以UTC格式存储时间,但是为了更改时区,我首先需要将其解析为ZonedDateTime
.
I need to store the times in UTC format, but in order to change the timezone, I first need to parse it as a ZonedDateTime
.
将"+0100"
转换为"+01:00"
很容易.但是,如何将(创建/更新的)_time解析为ZonedDateTime
,以便将其转换为UTC?
To convert "+0100"
to "+01:00"
is easy enough. But how can I parse the (created/updated)_time into a ZonedDateTime
so I can convert it to UTC?
推荐答案
好,让我分解一下问题陈述.
Well let me break down the problem statement.
首先,如果您要查询API,则可以假定它们遵循某种标准的Date-Time格式(即使您正在创建一个).查看给定的日期,就像他们遵循的那样-** ISO 8601 -日期和时间格式**
First if you are querying an API then it can be assumed that they are following some standard Date-Time format (even if you are creating one). Looking over the given date it looks like they follow - ** ISO 8601 - Date and time format **
所以问题是如何解析 ISO 8601-日期和时间格式
有哪些最佳选择?
- 使用 Joda时间.
//Joda
String jtDate = "2010-01-01T12:00:00+01:00";
DateTimeFormatter yoda = ISODateTimeFormat.dateTimeNoMillis();
System.out.println(parser2.parseDateTime(jtDate));
//using Java 8 (As you specified - To convert "+0100" to "+01:00" is easy enough.)
String strDate = "2017-01-05T16:32:29+01:00";
DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_DATE_TIME;
TemporalAccessor convertMe = timeFormatter.parse(strDate);
Date date = Date.from(Instant.from(convertMe));
System.out.println(date);
希望它会有所帮助:)
Hope it helps :)
这篇关于从JSON字符串确定TimeZone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!