0

I'm using Jackson to deserialize a date into this attribute:

private Date createDate; 

Part of a payload:

"createdDate": "1979-12-05T08:00Z",

Getting this error:

Can not deserialize value of type java.util.Date from String "1979-12-05T08:00Z": not a valid representation (error: Failed to parse Date value '1979-12-05T08:00Z': Can not parse date "1979-12-05T08:00.000Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails

What I tried so far was to include this dependency:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

and also:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-ddTHH:mmZ")
private Date createDate; 

but it didn't work.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
Julio
  • 429
  • 2
  • 16
  • I recommend you use the modern `Instant`, not the poorly designed and long outdated `Date` class. Together with the jsr310 dependency I also expect that this can fix your problem. – Ole V.V. Apr 25 '20 at 07:17

1 Answers1

0

JsonFormat annotation should work, you need to only fix your pattern to:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm'Z'")
private Date createDate;

Also, you should set time zone on ObjectMapper object:

ObjectMapper mapper = new ObjectMapper();
mapper.setTimeZone(TimeZone.getTimeZone("GMT"));

Take a look at:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146