Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonFormat converts Date with incorrect timezone

I have a simple POJO with a Date field with initial value coming in:

1985-09-17T01:00:00.000+0400

then this Date value gets mapped to a DTO with the Date field annotated:

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

Then the result is shown:

1985-09-16T21:00:00Z

I have tried setting the timestamp property in @JsonFormat, but that didn't help and the date is still invalid.

How can I correctly convert the date?

like image 874
Deniss M. Avatar asked Jun 29 '26 18:06

Deniss M.


1 Answers

The value within a java.util.Date is the number of milliseconds since the Unix epoch, which occurred at midnight January 1st 1970, UTC. As it's a number of milliseconds since a fixed epoch, the value within java.util.Date is the same around the world at any particular instant, regardless of local time zone.

So in your case it's better to use ZonedDateTime class if you use java 8 ZonedDateTime

like image 183
codeformars Avatar answered Jul 01 '26 06:07

codeformars



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!