File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes setting Timezone for web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "setting Timezone for web application" Watch "setting Timezone for web application" New topic
Author

setting Timezone for web application

Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
Is there a way to set time-zone for a web application(one-time at the time of web application start-up) ?

I got solutions like write a filter and set the timezone for every request, which doesn't seem proper.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
Are you asking about TimeZone objects? The JVM has a default, which can be set with TimeZone.setDefault, but that would apply to the complete JVM, not just to one particular web app.

Anything that's done just once for a web app should go in the contextInitialized method of a ServletContextListener.


Ping & DNS - updated with new look and Ping home screen widget
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
Thanks for the reply. But, Is there any particular method by which we can set timezone for that webapplication only.


If we call TimeZone.setDefault, from contextInitialized method of a ServletContextListener, I think it will still set timezone for entire JVM.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
What does it mean to set a timezone for one particular web application? If you need a particular TimeZone object for a web app, you can create it and store it in the web app context for all web app code to use.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

The general style is to let the server that is hosting the Web container keep the time. I keep all of my servers on UTC/GMT.

Then each user can tell you his local TZ, and most browsers do it automatically. With the Locale support, it can even properly change presentation for you.

This is a previously well solved problem, and I hate working on them again.
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
timezone should not be changed from user's timezone, it just that webapplication timezone should be configurable.

In servlet I set, request.setAttribute("currentdate",new Date());

in jsp, i it using display $(currentdate} , this should be in the updated timezone
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
The Date class has no notion of timezones. If you want to use timezones consistently then you need to use the Calendar object.

Also be aware that in a web app the "user timezone" can be different than the "system timezone".
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
The Date class has no notion of timezones


As an independent date class, it is not having notion of TimeZone. But, When you put that into JSP it is having timezone notion.

In the following code, currentdate being shown in the timezone I've set using TimeZone.setDefault()

In servlet I set, request.setAttribute("currentdate",new Date());

in jsp, i it using display $(currentdate} , this should be in the updated timezone


But, It is changing for all the webapplications ( entire JVM). Is there any other way of doing it for a single web application.
(Without using calenders)?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
Pratap koritala wrote:As an independent date class, it is not having notion of TimeZone. But, When you put that into JSP it is having timezone notion.

No it does not. See the source code of the Date.toString method for how the timezone is added on. It's highly likely that it does something other than what you expect or what you desire.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

Pratap koritala wrote:But, It is changing for all the webapplications ( entire JVM). Is there any other way of doing it for a single web application.
(Without using calenders)?


Modern Java code should use Calendars, not Date. The Date class is too simplistic to do much of what professionals need, especially in the area of Internationalization.

The Calendars class is not sufficient, either, you need all the DateFormat stuff.

Its all a mess caused by Java's initial design space, and the insistence on not breaking old code.
 
 
subject: setting Timezone for web application
 
Similar Threads
want client's time zone from server side
DST Problem on client side
capturing timezone from a browser???
JSF/RichFaces calendar; value loses one day
want client's time zone from server side