• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query regarding timezone  RSS feed

 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all genius people,

In my project I need to fetch the timezone of different location.
The module should run whenever new user register.

Is it possible to fetch the timezone in JSP.

[devaka: removed large text formatting]
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you can get it through Javascript.
 
Ayoma Wijethunga
Ranch Hand
Posts: 38
Firefox Browser IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there!

JSP is a server side language. So what ever you write in a JSP page will only get executed in the server you are going to host the JSP.

It is possible to get the TimeZone using Java (or JSP) using below code. But it is not useful, because it'll always return the server's TimeZone where the code is been executed.



So you can simply use a JavaScript to grab the client's time zone and send it to a JSP or a Servlet using an AJAX resuest.



Now, you can use java.util.TimeZone class, to get the actual timezone out of this Time Zone Offset. To do so, use below code in Java.



Hope this helps..
Regards.
 
Devaka Cooray
Marshal
Posts: 5325
575
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really need to extract the TimeZone of each user and store it on the database?
If you was about 'displaying' the correct local time for each user, you can simply use JavaScript:

.. and Ayoma, Welcome to JavaRanch
 
Ayoma Wijethunga
Ranch Hand
Posts: 38
Firefox Browser IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:Do you really need to extract the TimeZone of each user and store it on the database?


Hum.. Could be useful in a CMS or a Forum..

Devaka Cooray wrote:.. and Ayoma, Welcome to JavaRanch


Thanks for the welcome bro!
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thank you very much..


And
To,
Devaka Cooray

Yes, I need to store the timezone into database.

Any suggestion other then of Ayoma Wijethunga's

Thank you once again
 
Devaka Cooray
Marshal
Posts: 5325
575
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hardik Trivedi wrote:Any suggestion other then of Ayoma Wijethunga's

You may track the country of the user from the IP, and determine the TimeZone for that country.
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could be multiple timezones in a single country..
so that approach has limitations.

I feel, using javascript + Ajax + Servlet option is the most straightforward for this requirement.
 
Ayoma Wijethunga
Ranch Hand
Posts: 38
Firefox Browser IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiran Joshi wrote:There could be multiple timezones in a single country..
so that approach has limitations.

I feel, using javascript + Ajax + Servlet option is the most straightforward for this requirement.


Agreed! Also there could be clients who are using VPNs, public, coporate or private proxy servers. So, IP based mechanism will not identify timezones correctly. Application will store proxy server's timezone instead of client's.

Multiple timezones in a single country will be an issue, specially if the client's ISP is using NAT servers. In such case the IP of the NAT server will be checked, leading you to store the timezone of NAT server.

Anyway javascript + Ajax + Servlet will fail if client has JavaScript disabled. However I think it is better than just identifying timezone incorrectly. So you can ask the user to select the timezone, if JavaScript is disabled.

Or else use both methods. Use IP based mechanism if JavaScript is disabled.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could just ask the user to tell you their preferred timezone, and store this in the user's profile for the web site. You could use the other methods to guess what the user's timezone is so you can provide a reasonable default/initial value.
 
Khalil Salman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you guys think about this solution?

Locale cLoc=request.getLocale();
Calendar cCal=Calendar.getInstance(cLoc);
TimeZone tz=cCal.getTimeZone();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!