简体   繁体   中英

DateTimeParseException by parsing a date string

I'm trying to parse a date string with Java OffsetDateTime but I get exceptions.

The string looks like this: "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

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.

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):

yyyyMMddHHmmss'GMT'Z

Ideone demo

Please try

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

This could work. But I am thinking that zone and offset conflict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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