• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Query regarding timezone

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 7254
1395
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7254
1395
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Hang a left on main. Then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic