When server and client are in different time zones, can i get client's time zone in java using HttpServletRequest?
I am trying to create an instance of 'Calender' using client's 'Locale' like this,
Calendar calendar = Calendar.getInstance(request.getLocale());
TimeZone clientTimeZone = calendar.getTimeZone();
But this is giving me Server's time zone only.
Is this method wrong? Is there any other way to get Client's time zone in Server?
Calendar calendar = Calendar. getInstance(request. getLocale()); TimeZone clientTimeZone = calendar. getTimeZone();
The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes. This offset is changed by dividing by 60 and negating the result.
Retrieve the current time zone from the given request, using the TimeZoneAwareLocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the system's default time zone. Note: This method returns null if no specific time zone can be resolved for the given request.
Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.
Unfortunately the time zone information's are not passed in HTTP request
.
But there are a work around for this case.
Check this
answer and this
one. it may help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With