Java 无法解析的日期:“2013-07-11T13:41:22.000Z”(偏移量 23)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18483314/
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-12 02:02:54  来源:igfitidea点击:

Unparseable date: "2013-07-11T13:41:22.000Z" (at offset 23)

java

提问by Eugene

Can anybody tell me why in the world I got this exception?

谁能告诉我为什么我有这个例外?

08-28 08:47:05.246: D/DateParser(4238): String received for parsing is 2013-08-05T12:13:49.000Z

08-28 08:47:05.246: D/DateParser(4238): 收到的解析字符串是 2013-08-05T12:13:49.000Z

private final static String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";


public static Date parseDate(String stringToParse) {
        Date date = null;
        try {
            date = new SimpleDateFormat(DATE_FORMAT_PATTERN).parse(stringToParse);
        } catch (ParseException e) {
            Logger.logError(TAG, e);
        }
        return null;
    }

08-28 08:47:05.246: E/DateParser(4238): Exception: java.text.ParseException: Unparseable date: "2013-08-05T12:13:49.000Z" (at offset 23)

采纳答案by Scary Wombat

try using

尝试使用

String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

The Zat the end is usually the timezone offset. If you you don't need it maybe you can drop it on both sides.

所述Z在端部通常是时区偏移。如果你不需要它,也许你可以把它放在两边。

回答by Daniel S.

The Zin your time string is not a valid timezone identifier, but the time format you specified expects a time zone identifier there. More specifically, it expects a RFC 822 timezoneidentifier, which is usually 4 digits long.

Z你的时间字符串不是有效的时区标识符,但时间格式指定预计的时区标识符存在。更具体地说,它需要一个RFC 822 时区标识符,通常为 4 位数字。

回答by Jesper

Use Xinstead of Zat the end of the format string:

在格式字符串的末尾使用X而不是Z

yyyy-MM-dd'T'HH:mm:ss.SSSX

to parse ISO-8601 format timezone offsets.

解析 ISO-8601 格式时区偏移量。

(Only works if you use Java 7. See this question).

(仅当您使用 Java 7 时才有效。请参阅此问题)。

回答by Deadpool

From java-8 you can directly use ZonedDateTimeor Instantif it is in ISO_INSTANT

从 java-8 可以直接使用ZonedDateTime或者Instant如果它在ISO_INSTANT 中

ZonedDateTime.parse("2013-08-05T12:13:49.000Z")

Instant.parse("2013-08-05T12:13:49.000Z")