1

In my application I added REST. To test these methods I created some basic rows in my database.

When I try to use GET with an ID to get a specific row in my database I get the jackson exception.

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2019-03-11T10:14:14Z[UTC]": not a valid representation (error: Failed to parse Date value '2019-03-11T10:14:14Z[UTC]': Cannot parse date "2019-03-11T10:14:14Z[UTC]": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))

My object that I try to make has a field

@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date published;

And the constructor that gets called is

public something(Date published) {
    this.published = published;
}

I read in a StackOverflow question I should add the

@JsonType(pattern="")

above the field 'Published' but I can't seem to find what I need to import to get that annotation to work.

EDIT: Thanks to the answer from SHAHAKASH I managed to add the annotation after all.

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = TimeZone.getDefault(), locale = Locale.getDefault())

However I'm having two issues with this, one is that it won't allow me to keep the timezone and the locale. So removing those lets my program run

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")

Which gives me the error:

Cannot deserialize value of type `java.util.Date` from String "2019-03-11T10:14:14Z[UTC]": expected format "yyyy-MM-dd'T'HH:mm:ss.SSS"

Obviously meaning my format is wrong. I'm having lots of troubles to find the correct format of my Date.

Déjà vu
  • 774
  • 2
  • 9
  • 31
  • 1
    https://stackoverflow.com/questions/17655532/json-serializing-date-in-a-custom-format-can-not-construct-instance-of-java-uti – Akash Shah Mar 13 '19 at 11:10
  • @SHAHAKASH Hey, thank you for the answer! That reduced my error quite a bit. But I'm having lots of troubles to find the correct pattern. Could you please take a look at my edit and find out what pattern I need? – Déjà vu Mar 13 '19 at 11:26
  • 2
    side note: not use Date class that is outdated use LocalDate – Akash Shah Mar 13 '19 at 11:30
  • 1
    problem is in your code last `z[UTC]` replace with `"'yyyy-MM-dd'T'HH:mm:ss.SSS'Z"` may solve your problem. – Akash Shah Mar 13 '19 at 11:34
  • @SHAHAKASH Hey, thanks for your help. I feel terrible for bothering you so much. But now I get yet another error. Which reads "Illegal pattern character 'T'" – Déjà vu Mar 13 '19 at 11:47
  • 1
    https://stackoverflow.com/q/26398657/11032808 see answer – Akash Shah Mar 13 '19 at 11:55
  • @SHAHAKASH I did find this answer yes, and adding the fix gives me the "Unparseable date: "2019-03-11T10:14:14Z[UTC]"" exception. Which a user in the comments also notes. – Déjà vu Mar 13 '19 at 12:08
  • I recommend you don’t use `Date`. That class is poorly designed and long outdated. Don’t you want `Instant` or perhaps `ZonedDateTime`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and [jackson-modules-java8](https://github.com/FasterXML/jackson-modules-java8)? – Ole V.V. Mar 13 '19 at 14:59

1 Answers1

3

try this pattern yyyy-MM-dd'T'HH:mm:ss'Z'

String testDate = "2019-03-11T10:14:14Z[UTC]";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = sdf.parse(testDate);
System.out.println(d);

Output :

Mon Mar 11 10:14:14 IST 2019
Akash Shah
  • 596
  • 4
  • 17
  • 1
    After a lot of problems (on my side) I finally tried to implement your solution. And it works, I actually can't thank you enough. This caused me so many problems/headaches/stress and whatnot. But you actually figured it out. So thanks again man! You really helped someone out today. – Déjà vu Mar 13 '19 at 13:21
  • 1
    That output is wrong. `2019-03-11T10:14:14Z[UTC]` denotes the same point in time as Mon Mar 11 **15:44**:14 IST 2019 (assuming IST stands for India Standard Time). – Ole V.V. Mar 14 '19 at 12:33
  • Don’t use `Date` nor `SimpleDateFormat`. Ignoring the fact that this is to be used with Jackson, it’s as simple as `ZonedDateTime dateTime = ZonedDateTime.parse(testDate);` (no explicit formatter needed). With Jackson I believe you want [jackson-modules-java8](https://github.com/FasterXML/jackson-modules-java8). – Ole V.V. Mar 14 '19 at 12:48