0

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()
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Jonathan Chiou
  • 349
  • 6
  • 15
  • One `z` is enough. – akuzminykh Sep 19 '20 at 19:38
  • Thanks for the info! Unfortunately, that doesn't seem to be the issue as I'm still failing to parse the String. – Jonathan Chiou Sep 19 '20 at 19:40
  • 1
    Can't reproduce [demo](https://onlinegdb.com/r1gpHgkVHP) – Eklavya Sep 19 '20 at 19:47
  • 3
    Maybe this topic can help you. https://stackoverflow.com/questions/44743139/trying-to-parse-a-datetime-in-pdt-to-a-zoneddatetime-representation – Arthur Maciel Gomes Sep 19 '20 at 19:48
  • 1
    @Rono I'm running the code in an Android ecosystem with the Android port https://github.com/JakeWharton/ThreeTenABP: perhaps that might have an impact on things – Jonathan Chiou Sep 19 '20 at 19:59
  • No, @ArthurMacielGomes, Jonathan Chiou is already doing what the answers there say (specifying the format through a formatter). – Ole V.V. Sep 19 '20 at 20:53
  • Tried it on ThreeTen Backport (not the Android adaptation, though). Got `2020-09-19T07:14-07:00[Canada/Yukon]`, a slight bit surprised about the choice of time zone, but I didn’t get any exception, – Ole V.V. Sep 19 '20 at 20:59
  • 2
    It’s weird. Sorry, I haven’t got any suggestions. – Ole V.V. Sep 19 '20 at 21:06
  • Which result do you get when you try formatting a `ZonedDateTime` in summer in, say, America/Montreal time zone, using the same formatter? Asking because index 25 is where `PDT` is, so the time zone abbreviation seems to be the problem. Also did you check your string for any non-printing characters? – Ole V.V. Sep 20 '20 at 11:57

1 Answers1

0

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]
WJS
  • 36,363
  • 4
  • 24
  • 39
  • 2
    I'm doing that already: I updated my code to use a hardcoded "Sat Sep 19 2020 07:14 AM PDT" instead and the issue still persists. – Jonathan Chiou Sep 19 '20 at 20:00
  • The code's contained in a moshi adapter: ``` 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() } } ``` – Jonathan Chiou Sep 19 '20 at 20:18