• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting Timezone for web application

 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic