0

Why can't I use EEE MMM dd HH:mm:ss Z yyyy to deserialize Wed Mar 09 14:02:57 +0800 2022? Is there anything wrong?

I am using Jackson and I added this annotation in my entity class

@JsonFormat(pattern="EEE MMM dd HH:mm:ss Z yyyy")
private Date created_at;

However, I got this error:

Cannot deserialize value of type java.util.Date from String "Wed Mar 09 14:02:57 +0800 2022": expected format "EEE MMM dd HH:mm:ss Z yyyy"

Matt
  • 12,848
  • 2
  • 31
  • 53
Dax1
  • 11
  • 2
  • 5
    I recommend you don’t use `java.util.Date`. That class is poorly designed and long outdated. Instead use `OffsetDateTime` or another class from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 17 '22 at 08:24
  • 4
    I can get both `DateTimeFormatter`/`ZonedDateTime` and `SimpleDateFormat` can parse the value you've provided. I'd suspect that issue has something to do with "how" you're parsing the text – MadProgrammer Mar 17 '22 at 08:24
  • 2
    Could it be a locale problem? `Wed` and `Mar` are in English, so if your library expects a different language, it would explain. What is the default locale of your JVM? What happens if you change it to an English-speaking locale? – Ole V.V. Mar 17 '22 at 08:27
  • 2
    As shown in the duplicate, add `locale="en"` to the annotation. – Mark Rotteveel Mar 17 '22 at 08:29
  • I changed the locale to English then it works, so thank you, everyone!! – Dax1 Mar 17 '22 at 09:34

1 Answers1

0

I recommend to escape from that format and use ISO 8601 instead.

Also, it's better to use the classes in the java.time package.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Barrrettt
  • 645
  • 7
  • 15