JSON 日期到 Java 日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4032967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:45:56  来源:igfitidea点击:

JSON date to Java date?

javajson

提问by Felix

I could not find this anywhere. I fetch some JSON from an API that returns standard JSON dates. You can see the format by running this code in a JavaScript console:

我在任何地方都找不到这个。我从返回标准 JSON 日期的 API 中获取一些 JSON。您可以通过在 JavaScript 控制台中运行此代码来查看格式:

> new Date().toJSON();
"2010-10-27T11:58:22.973Z"

Well, actually, the API I'm working with is not returning the millisecond part, and sometimes it returns a timezone instead of Z, so dates can look like any one of these:

嗯,实际上,我正在使用的 API 没有返回毫秒部分,有时它返回一个时区而不是Z,所以日期可能看起来像以下任何一个:

  • 2010-10-27T11:58:22Z
  • 2010-10-27T11:58:22+03:00
  • 2010-10-27T11:58:22Z
  • 2010-10-27T11:58:22+03:00

Parsing these kinds of dates is somewhat cumbersome. Is there any way to parse these kinds of dates, using org.json?

解析这些类型的日期有点麻烦。有什么方法可以解析这些类型的日期,使用org.json?

My current solution is:

我目前的解决方案是:

public static Date parseDateTime(String dateString) {
    if (dateString == null) return null;
    DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
    if (dateString.contains("T")) dateString = dateString.replace('T', ' ');
    if (dateString.contains("Z")) dateString = dateString.replace("Z", "+0000");
    else
        dateString = dateString.substring(0, dateString.lastIndexOf(':')) + dateString.substring(dateString.lastIndexOf(':')+1);
    try {
        return fmt.parse(dateString);
    }
    catch (ParseException e) {
        Log.e(Const.TAG, "Could not parse datetime: " + dateString);
        return null;
    }
}

Ugh!

啊!

采纳答案by codelark

That DateTime format is actually ISO 8601 DateTime. JSON does not specify any particular format for dates/times. If you Google a bit, you will find plenty of implementations to parse it in Java.

该日期时间格式实际上是 ISO 8601 日期时间。JSON 没有为日期/时间指定任何特定格式。如果你谷歌一下,你会发现很多实现可以用 Java 解析它。

Here's one

这是一个

If you are open to using something other than Java's built-in Date/Time/Calendar classes, I would also suggest Joda Time. They offer (among many things) a ISODateTimeFormatto parse these kinds of strings.

如果您愿意使用 Java 内置的 Date/Time/Calendar 类以外的其他东西,我也建议使用Joda Time。他们提供(其中包括) aISODateTimeFormat来解析这些类型的字符串。

回答by Jon Freedman

If you need to support more than one format you will have to pattern match your input and parse accordingly.

如果您需要支持多种格式,则必须对输入进行模式匹配并相应地进行解析。

final DateFormat fmt;
if (dateString.endsWith("Z")) {
    fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
} else {
    fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
}

I'd guess you're dealing with a bug in the API you're using which has quoted the Ztimezone date pattern somewhere...

我猜你正在处理你正在使用的 API 中的一个错误,它在Z某处引用了时区日期模式......

回答by Chuck

Note that SimpleDateFormat format pattern Z is for RFC 822 time zone and pattern X is for ISO 8601 (this standard supports single letter time zone names like Z for Zulu).

请注意,SimpleDateFormat 格式模式 Z 适用于 RFC 822 时区,模式 X 适用于 ISO 8601(该标准支持单字母时区名称,如 Z 代表祖鲁语)。

So new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")produces a format that can parse both "2013-03-11T01:38:18.309Z"and "2013-03-11T01:38:18.309+0000"and will give you the same result.

Sonew SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")产生一种可以解析"2013-03-11T01:38:18.309Z"和的格式,"2013-03-11T01:38:18.309+0000"并且会给你相同的结果。

Unfortunately, as far as I can tell, you can't get this format to generate the Z for Zulu version, which is annoying.

不幸的是,据我所知,您无法获得这种格式来为 Zulu 版本生成 Z,这很烦人。

I actually have more trouble on the JavaScript side to deal with both formats.

实际上,我在 JavaScript 方面遇到了更多的麻烦来处理这两种格式。