简体   繁体   English

Gson:java.text.ParseException:无法解析的日期:“ 2018-04-09T09:00:00 + 02:00”

[英]Gson: java.text.ParseException: Unparseable date: “2018-04-09T09:00:00+02:00”

How can I parse a string date in the format: 如何解析以下格式的字符串日期:

"2018-04-09T09:00:00+02:00"

Gson uses: Gson使用:

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)

But it gives the following exception: 但这给出了以下例外:

    com.google.gson.JsonSyntaxException: 2018-04-09T09:00:00+02:00
    at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:107)
    at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:82)
    at com.google.gson.DefaultDateTypeAdapter.deserialize(DefaultDateTypeAdapter.java:35)
    at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183)
    at com.google.gson.Gson.fromJson(Gson.java:805)
    at com.google.gson.Gson.fromJson(Gson.java:743)
    ... 35 more
Caused by: java.text.ParseException: Unparseable date: "2018-04-09T09:00:00+02:00"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at com.google.gson.DefaultDateTypeAdapter.deserializeToDate(DefaultDateTypeAdapter.java:105)
    ... 52 more

Thanks in advance. 提前致谢。

You put the Z inside quotes ( 'Z' ). 您将Z放在引号( 'Z' )中。 If you take a look at the javadoc , you'll see that: 如果看一下javadoc ,您将看到:

Text can be quoted using single quotes (') to avoid interpretation 文本可以使用单引号(')进行引用以避免解释

This means that your formatter is expecting the letter Z (and not some other value like +02:00 ), and that's why you're getting the error. 这意味着格式化程序期望字母Z (而不是+02:00类的其他值),这就是为什么会出现错误。

In the same javadoc page we can see that the pattern letter to parse offsets (the +02:00 part) is X , so your formatter should be like this: 在同一javadoc页面中,我们可以看到用于解析偏移量的模式字母( +02:00部分)为X ,因此格式化程序应如下所示:

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.US);

As noticed in the comments, the pattern X was introduced only in java 7. In older versions, the only way is to split the string and set the offset in the formatter as a TimeZone : 正如评论中所注意到的,模式X仅在Java 7中引入。在较早的版本中,唯一的方法是拆分字符串并将格式化程序中的偏移量设置为TimeZone

String input = "2018-04-09T09:00:00+02:00";
Pattern pattern = Pattern.compile("(.*)([\\+|\\-]\\d{2}:\\d{2})");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
    // timezone will be GMT+02:00
    sdf.setTimeZone(TimeZone.getTimeZone("GMT" + matcher.group(2)));
    // parse date without the offset part
    Date date = sdf.parse(matcher.group(1));
}

As said in the comments, you can also use the threeten backport: http://www.threeten.org/threetenbp/ 如评论中所述,您还可以使用Threeten backport: http : //www.threeten.org/threetenbp/

That's a backport to java 8's date/time classes, and it's much better and easier to use: 这是对Java 8的日期/时间类的反向移植,并且使用起来更好,更容易:

OffsetDateTime odt = OffsetDateTime.parse("2018-04-09T09:00:00+02:00");

And if you still need to use java.util.Date , is easy to do the conversion: 而且,如果您仍然需要使用java.util.Date ,则很容易进行转换:

Date date = DateTimeUtils.toDate(odt.toInstant());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.text.ParseException:无法解析的日期:“ 1991-04-14 00:00:00” - java.text.ParseException: Unparseable date: “1991-04-14 00:00:00” java.text.ParseException:无法解析的日期:“ 2015-08-19T00:00:00” - java.text.ParseException: Unparseable date: “2015-08-19T00:00:00” java.text.ParseException:无法解析的日期:“ 2018年10月7日,星期日,11:00 PM” - java.text.ParseException: Unparseable date: “11:00 PM, Sun 07 Oct 2018” java.text.ParseException:无法解析的日期:“1901-01-01 00:00:00” - java.text.ParseException: Unparseable date: “1901-01-01 00:00:00” java.text.ParseException:无法解析的日期:“ CET 2012年1月11日星期三00:00:00” - java.text.ParseException: Unparseable date: “Wed Jan 11 00:00:00 CET 2012” typeMismatch.target spring boot 批量拒绝值 [2020-09-18T00:00:00+02:00] - typeMismatch.target spring boot batch rejected value [2020-09-18T00:00:00+02:00] java.text.ParseException:无法解析的日期:“ 2012年1月11日08:00:00” - java.text.ParseException: Unparseable date:“01/11/2012 08:00:00” java.text.ParseException:无法解析的日期:“ 04-02-2019” - java.text.ParseException: Unparseable date: “04-02-2019” 不可解析的日期java.text.ParseException:不可解析的日期:“ 2015-10-08 05:00:00.0” - Unparsable Date java.text.ParseException: Unparseable date: “2015-10-08 05:00:00.0” java.text.ParseException:无法解析的日期:“Thu Jan 19 2012 08:00 PM” - java.text.ParseException: Unparseable date: “Thu Jan 19 2012 08:00 PM”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM