0
LocalDateTime time = LocalDateTime.parse("27. April 2021 00:03:50 MESZ", DateTimeFormatter.ofPattern("dd. MMMM yyyy HH:mm:ss z"));

Trying to parse time string as shown above. 'z' in the pattern is not able to recognize "MESZ" time zone.

Can someone tell me if there is any other pattern I have to use to match "MESZ"?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Vishal
  • 29
  • 5
  • `MESZ` is not in the standard list of [timezone names](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones), although it's used frequently in Germany. You'll probably have to special-case a replacement with `CEST` for it. – Gereon Mar 25 '23 at 12:37
  • 1
    Thank you @Gereon. I also found the answer that I have to use `Locale.Germany` – Vishal Mar 25 '23 at 18:03
  • 1
    Do not parse into a `LocalDateTime` here. Your string contains a time zone abbreviation that you want to pick up. So use `ZonedDateTime`. You can always convert later if you need to, and take the time zone into account in that conversion, but for most purposes you will not need to. – Ole V.V. Mar 26 '23 at 12:28
  • Does this answer your question? [java DateTimeFormatterBuilder fails on testtime](https://stackoverflow.com/questions/50526234/java-datetimeformatterbuilder-fails-on-testtime) – Ole V.V. Mar 26 '23 at 12:31

1 Answers1

2

I found the answer. I have to use DateTimeFormatter with Locale

DateTimeFormatter FORMATTER = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withLocale(Locale.GERMANY);
LocalDateTime time = LocalDateTime.parse("27. April 2021 00:03:50 MESZ", FORMATTER);
Vishal
  • 29
  • 5