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.
Related
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
Is there a way to get today's date and time such that it corresponds to the real world date/time and is not affected if the user has changed the date/time settings on their phone/web browser?
If not, is using a server time the best way to correctly determine today's date on the phone? Or are there other best practices?
if you don't want to use a server time, u can parse the return of gettime() link
Server time suits most of the needs. Then if your server's time is messed up then you will be in problem.
Alternatively, you can use some third party web service to provide you with the time.
For example
https://developers.google.com/maps/documentation/timezone/
Google being a reputed company, the time returned can be trusted to be correct.
I'd never trust a user's device to retrieve time related information.
Get the UTC time from the server and if required, display it to the user converted to his time zone. Here's a so question on how to convert UTC time to local time with JS
This way your stored time will always be ok and in same "format". The only thing that could happen is that the ends up seeing a "wrong" time, if he faked his location / time zone settings. But I wouldn't mind that.
I want to basically monitor the server calls on java side. So is there any way I can directly obtain the timestamp when request was received .
In other words,I do not want to explicitly make Date Objects and find out the timestamp. Rather I am interested if there's any timestamp parameter utility provided with Request Object.
Something provided by ServletActionContext.getRequest() or any available solution without the need to code to create Date() objects and find out.
The reason I am saying is I have around 50-60 server calls and may be even more in future in my application so each time i need code in application layer rather use this ready-made facility.
HttpServletRequest.getDateHeader gives you a timestamp as a long value.
Although this is from the request header and not the receieved time, it might work since it provides milliseconds since the epoch.
You can also look into Filter, which allows you to
Examples that have been identified for this design are:
1. Authentication Filters
2. Logging and Auditing Filters
...
Event Listeners might be another facility, where you can hook into the processing of a servlet.
Back to your question:
ServletActionContext.getRequest() -> HttpServletRequest.getSession() ->
HttpSession.getLastAccessedTime
Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT, and marked by the time the container received the request.
In a Servlet, I'll like to obtain the datetime when a request is made.
I don't want to use Java's Date Class because it could not provide the accurate time.
Any help?
You have three choices:
Use client's computer time. This will require the client to send his or her system time explicitly in the request parameters or in a custom header. Not to mention arbitrary computer in the Internet might have much more inaccurate time.
Use external time servers to fetch current time.
...or just trust your server, if it uses ntp, you are on the safe side.
I'm developing a client-server application on java. The server offers some operations through a web service. The application must show some content depending on the user's custom date and time; information that is retrieved from the web service. Different users may be in different time zones.
I've been thinking on how to store on the server the user's custom time zone and provide the user the right content, for example, when the operation getTodayEvents is invoked.
What's the best practices to accoplish this in java?
Yes, You can store the timezone info into your user's details table. And according to that you can serve from your web service. I think this is the best way to do it.
There are two ways to do this:
1) Get the user to select a timezone in their account setup, and store that with the user details.
The problem with this approach is that the user login from different timezones, dealing with daylight saving time can be complicated, and you have to implement a user-friendly mechanism for selecting time zone.
2) Put the following or something like it into the login page, and have the submit action send the value of tzo to the server as a (hidden) form parameter.
<script type="text/javascript" language="javascript">
<!--
var tzo=(new Date().gettimezoneOffset()/60)*(-1);
// -->
</script>
This neatly avoids the problems of the first approach. The only problem is that it won't work if the user has disabled Javascript.
A word of advice: when your code has significant date-related logic (and if it accounts for timezones, it has), spare yourself a world of pain and use Joda time. Unlike Java's unholy union of Date and Calendar, the API of Joda Time generally nudges you towards doing things the right way.