简体   繁体   English

DateTimeParseException通过解析日期字符串

[英]DateTimeParseException by parsing a date string

I'm trying to parse a date string with Java OffsetDateTime but I get exceptions. 我正在尝试使用Java OffsetDateTime解析日期字符串,但出现异常。

The string looks like this: "20101217180000GMT+0800" 字符串如下所示: "20101217180000GMT+0800"

My approach looks like this: 我的方法如下所示:

OffsetDateTime.parse("20101217180000GMT+0800", DateTimeFormatter("yyyyMMddHHmmssz"));

I get: 我得到:

java.time.format.DateTimeParseException: .... unparsed text found at index 17 java.time.format.DateTimeParseException:....在索引17处找到未分析的文本

Any ideas? 有任何想法吗? :) :)

You basically have two zone specifiers there: 那里基本上有两个区域说明符:

  • GMT
  • +0800

If you print the substring of the time starting at index 17, the place where the error occurs, you get +0800 , so it has consumed the GMT and doesn't know what to do with the rest. 如果打印从索引17开始的时间的子字符串(发生错误的位置),则会得到+0800 ,因此它已经消耗了GMT ,并且不知道如何处理其余时间。

I would suggest handling the GMT as a literal, ie surround it in single quotes, and use Z (or X , depending on how you'd want to handle GMT+0, if ever): 我建议将GMT作为文字处理,即用单引号将其括起来,并使用Z (或X ,取决于您要如何处理GMT + 0,如果有的话):

yyyyMMddHHmmss'GMT'Z

Ideone demo Ideone演示

Please try 请试试

OffsetDateTime.parse("20101217180000GMT+0800",DateTimeFormatter.ofPattern("yyyyMMddHHmmsszx"));

This could work. 这可能有效。 But I am thinking that zone and offset conflict. 但是我认为该区域和抵消冲突。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM