Java(Spring) automatically correcting date by timezone bug - java

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!

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.

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
for more details

Related

Spring Boot 2 get client timezone [duplicate]

How to get client/request timezone in jsp?
Unfortunately this information is not passed in HTTP headers.
Usually you need cooperating JavaScript to fetch it for you.
Web is full of examples, here is one http://www.coderanch.com/t/486127/JSP/java/Query-timezone
you cannot get timezone, but you can get current time from client side.i.e. through javascript and than post back. On server side, you can convert that time to GMT/UTC. The UTC shows the TimeZone.
If you just need the local timezone in order to display local times to the user, I recommend representing all times in your service in UTC and rendering them in browsers as local times using Moment.js.
My general rule is to handle and store times in UTC everywhere except at the interface with the user, where you convert to/from local time. The advantage of UTC is that you never have to worry about daylight-saving adjustments.
Note that if you want to show the age of something (e.g. "posted 3 hours ago") you just need to compare the UTC timestamp with the current UTC time; no need to convert to local times at all.
Best solution for me is sending date/time as a string, and then parse with server's timezone to get a timestamp. Timestamps are always UTC (or supposed to be) so you will not need client's TimeZone.
For example, sending "10/07/2018 12:45" can be parsed like:
SimpleDateFormat oD = new SimpleDateFormat();
oD.applyPattern("dd/MM/yyyy HH:mm");
oD.setTimeZone(TimeZone.getDefault()); // ;)
Date oDate = oD.parse(request.getParameter("time"));
Obviously you can set your specific date/time format.

How do I turn off timezone adjustments for springboot 3?

Ive been going through many different post about this issue but seem unable to make any proposed solution work. I have added this:
spring.jackson.deserialization.ADJUST_DATES_TO_CONTEXT_TIME_ZONE =false
To my applications.properties file but this doesn't work. I tried implementing a custom deserializer but that doesn't seem to work. Does anyone know how to get spring to read the time fields straight fcrom the db without adjusting the timezone for each value?
EDIT: I have tried a new method and I actually changed the offset being applied to the output records. In my STarterApp class I added this:
#PostConstruct
void started() {
TimeZone.setDefault(null);
//TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
The null only applies an hour to each record where as using UTC applies about 5 hours. Is their a way I can set timezone to have spring not adjust the times returned to include timezone differences? I just want the time that was saved originally without the offset

How get client timezone offset in spring controller without using cookie?

I'm trying to show dates on page, now server time is shown, the way I found is set cookie value in browser and use it in controller, but first time page is loading cookie value isn't settled. The only possible way I see is use ajax, Is there any way get timezone offset in controller without using cookie?
Sorry but there is really no way of getting timezone offset on the server side unless you explicitly pass it from the client. A possible work around is on the server side pass all your dates to the client in some standard timezone (probably GMT) then use javascript to localize the page to the right timezone.
A library I have used in the past that makes these timzones manipulations much easier is called moment.js but it is not necessary for you to use.

Java gregorian calendar and removing time and timezone

I have a gregorian calendar that serializes to the string below during a soap request
2079-07-07T00:00:00.000-07:00
The .NET webservice reads it as the string below
07/07/2079 01:00:00
Is the 07:00 causing the issue? If so how can I get rid of this?
Someone might be able to give you an affirmative answer if you tell us which time zone is configured on your system, but I would assume a mismatch in the DST rules.
The JVM comes with its own timezone and DST rule database, while Windows (and .NET) uses a different database. In theory, the two databases should of course contain the same rules, but I have ran into differences in the DST rules for historical dates. I would assume there might be differences for dates far in the future as well.
If you actually want to transport a date value (no time component) over the SOAP service, the easiest solution would be to use the appropriate XML Schema datatype instead of a datetime type.
Use a DateTimeOffset in your .net code and it will work just fine.

Using timezone 'EST' in freemarker template

<#setting time_zone="America/New_York">
Time: ${response.currentDate?string("MM/dd/yyyy hh:mm a zzz")}.
I need the timezone to be displayed as 'EST'.
But currently, when i run the application and the email gets generated from the template above, it is displaying as 'EDT'.
Can you please let me know what needs to be done to show as 'EST'?
The current timezone (on the system where I am testing) is Indian Standard Time
Thanks!
try <#setting time_zone="US/Eastern">
The behavior is expected at this point of year.
Refer: http://www.timeanddate.com/library/abbreviations/timezones/na/edt.html
(thanks to the above comment by Jon)

Categories

Resources