简体   繁体   中英

org.threeten.bp.format.DateTimeParseException: Text 'Sat Sep 19 2020 07:14 AM PDT' could not be parsed at index 25 at $.root.bsa[0].posted

I'm trying to parse a date that looks like: Sat Sep 19 2020 07:14 AM PDT into a ZonedDateTime .

I'm using this formatter

DateTimeFormatter.ofPattern(
  "EEE MMM dd yyyy hh:mm a zzz",
  Locale.US
)

and I'm attempting to convert the String with

ZonedDateTime.parse(
  value,
  formatter
)

However, this is throwing the following exception: org.threeten.bp.format.DateTimeParseException: Text 'Sat Sep 19 2020 07:14 AM PDT' could not be parsed at index 25 at $.root.bsa[0].posted

I'm not really sure where the error in my date format String is: I've ran it through http://www.fileformat.info/tip/java/simpledateformat.html to verify, but it looks like that's formatting correctly on there.

Additional context: this code is being executed in a Moshi adapter and is being run in an Android app:

class ZonedDateTimeAdapter {
    private val formatter = DateTimeFormatter.ofPattern( "EEE MMM dd yyyy hh:mm a z", Locale.US )

    @FromJson
    fun fromJson(value: String): ZonedDateTime {
        return ZonedDateTime.parse( value, formatter )
    }

    @ToJson
    fun toJson(value: ZonedDateTime): String {
        return value.toString()
    }
}

Did you try it like this?

String s = "Sat Sep 19 2020 07:14 AM PDT";
ZonedDateTime z = ZonedDateTime.parse(s, DateTimeFormatter.ofPattern(
                "EEE MMM dd yyyy hh:mm a z"));
System.out.println(z);

Prints

2020-09-19T07:14-07:00[America/Los_Angeles]

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