developer1
developer1

Reputation: 557

Java(Spring) automatically correcting date by timezone bug

so I have a task to fix a bug. So basically I make a GET request for /report-information and get in response two fields: Date dateFrom, Date dateTill.

The correct date that it should return should be: 2019-07-01 00:00:00 and 2019-07-31 23:59:59 - that's the data from a database. Basically, I make a GET request for these fields, Java takes it from database and sends it back to me.

But the problem is that somehow the values that are returned after making a GET request are: 2019-06-30 21:00:00 and 2019-07-31 20:59:59. Basically -3 hours because of some automatic timezone correction.

What I need is to create a method or something similar that would make java ignore the timezone and wouldn't change the date. I should use Date variables not to mess up a lot of other code that uses these fields.

Also I should mention that when I debugged the whole process, somehow in the @RequestMapping method it returns these fields correctly, as they should be, but when I make a request through my browser or Postman, I get the corrected by timezone version of the date. But it is possible that I just missed something.

Do you have any suggestions or ideas why and at what moment does Java change the date automatically? And what should I do to prevent it from correcting the date.

Thank you!

Upvotes: 0

Views: 487

Answers (2)

Istiaque Hossain
Istiaque Hossain

Reputation: 2357

i also face this problem some day ago.... in my case it's arise from Jackson lib (i project is in spring boot)

to fix this problem i set my time zone for Jackson lib in application.property file

spring.jackson.time-zone:Asia/Dhaka

in my case i am in Dhaka Bangladesh

also try by this

enter image description here

for more details

Upvotes: 1

Shailendra
Shailendra

Reputation: 9102

Since you have mentioned that in your Controller code you can see the correct date, you may want to check the Jackson serializer ( assuming this is the one serializing objects to json ) settings used for serializing the date object into JSON. You can check the multiple options for configuration here.

Upvotes: 1

Related Questions