Yashavanta SB
Yashavanta SB

Reputation: 79

google appengine - Time zone changed

In ServletContextListener initialization method we are setting Time Zone as

public void contextInitialized(ServletContextEvent event) {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+00:00"));

}

But when i check the Time Zone information in servlets and filters the Time Zone got changed. Any one know what might be the reason.

Thanks

Upvotes: 0

Views: 534

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338684

I cannot address your Question specifically as you do not show us the code for how you get time zone. But I can give you some tips.

Your code should be working. The problem is likely to be in (a) how you are getting the time zone or (b) the default zone being set somewhere else.

Add a line of code before and after where you set the zone to get the zone so as to log the change taking effect. See this done in the Answer by Sheeran.

Use the modern java.time classes rather than the troublesome old legacy date-time classes.

The default time zone applies to the entire JVM. Any code in any thread of any app within the JVM can change the default at any moment at runtime. Such a change affects all code in all threads of all apps within the JVM. So relying on the JVM’s current default time zone is risky and ill-advised in general but especially so on a server and even more so on a Servlet container.

Furthermore, the best practice on servers is to keep the default time zone in UTC. Though, again, you should not rely on that default.

There is almost never a need to set the default. Instead, pass explicitly the desired/expected time zone as a ZoneId (or ZoneOffset) as the optional argument to the various methods in java.time. Frankly I wish all those zone arguments were required rather than optional as most programmers fail to think about the crucial issue of zones and offsets.

Instant instant = Instant.now() ;  // Current moment in UTC.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

Much of your work should be done in UTC rather than with zoned values. Programmers should generally be thinking, working, logging, exchanging data, and serializing data all in UTC. As a programmer, you should stop thinking parochially about your own personal time zone while on the job, as constant conversions in/out of that zone will muddy your thoughts, lead to errors, and drive you crazy. Generally you should assign zones only where expected by your user in presentation in the user-interface.

Upvotes: 1

sai
sai

Reputation: 419

See, I've following class

public class TimeZ {
public static void main(String args[]){
    System.out.println("1."+TimeZone.getTimeZone("GMT+00:00"));
    System.out.println("2. "+TimeZone.getDefault());
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+00:00"));
    System.out.println("3. "+TimeZone.getDefault());
    System.out.println("4. "+TimeZone.getTimeZone("GMT+00:00"));    
}
}

my output is:

1.sun.util.calendar.ZoneInfo[id="GMT+00:00",offset=0,dstSaving...

2. sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,...

3. sun.util.calendar.ZoneInfo[id="GMT+00:00",offset=0,dstSaving...

4. sun.util.calendar.ZoneInfo[id="GMT+00:00",offset=0,dstSaving...

explanation: by default my timezone is india. It's going to return the timezone of the JVM TimeZone.getDefault() is executed on. So if the application is running on a server in India, it will be something like "Asia/Calcutta" .when you set default timezone to GMT, it changes its timezone to GMT zone. thats simple...

Upvotes: 1

Related Questions