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()
    }
}
1

There are 1 answers

2
WJS On

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]