On client side, i use dd/MM/yyyy date format. The field use a twitter bootstrap 3 datetime picker (https://eonasdan.github.io/bootstrap-datetimepicker/)
I enter via twitter bootstrap 3 datetime picker 24/07/2015
in my json i sent, i see: birthdate: “24/07/2015”
In my dto, i do
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private Date birthdate;
When i receive the date on the server, in my dto see: 23/07/2015 19:00
One day is lost.
Any explication?
6
Answers
According to JacksonFAQDateHandling page:
In your case, it looks like the date is automatically being converted to GMT/UTC. Try to provide your local timezone explicitly to avoid UTC conversion [as mentioned in the question How come this time is off by 9 hours? (5 hours, 3 hours etc) on same page]:
Secondly, I think you are using
Date.toString()
to print date. Note that javaDate
class is timezone independent but itstoString()
method uses the system’s default timezone before printing.Here it looks like
24/07/2015 00:00 UTC
is being converted to23/07/2015 19:00 EST
bytoString()
. Both of these represent the same moment of time but in different timezones.AimZ answer is what pointed me to this but I added these three lines to my application.properties file and achieved the same thing
spring.jackson.date-format=yyyy-MM-dd
spring.jackson.serialization.write-dates-as-timestamps:false
spring.jackson.time-zone:EST
Had the same issue. Using postman to verify that the client is not the culprit. Seems like an issue with the timezone Jackson is using vs the timezone of the system. Had to change the Jackson config to compensate for dates
I had the same issue in JavaScript when selecting dates from a datepicker. I formatted the fields using .toString() method but the function gave me a different date (I was messing a day as well). Like this:
var mydate = new Date('2020-04-03');
console.log(mydate.toString());
//Thu Apr 02 2020 20:00:00 GMT-0400 (Eastern Daylight Time)
I fixed it ussing .toUTCString() instead.
I had the same issue in java. You can use ObjectMapper to set to default timezone
Solution here: Jackson @JsonFormat set date with one day less
Full configuration class:
I faced this same problem this week. For us the problem was the Decoder that we were using.
Our project is using feing and to decode our JSON we were using JacksonDecoder, so we changed to GsonDecoder, so problem solved.