Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Query regarding timezone

 
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]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you can get it through Javascript.
 
Ranch Hand
Posts: 38
IntelliJ IDE Firefox Browser 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.
 
Sheriff
Posts: 6601
1253
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • 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
IntelliJ IDE Firefox Browser 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
Sheriff
Posts: 6601
1253
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • 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
IntelliJ IDE Firefox Browser 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.
 
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.
 
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();
 
Well THAT's new! Comfort me, reliable tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic