Change Timezone in Tomcat

How to change Tomcat 7's server timezone?

With all of the different places where you can set timezones, it's (in general) always best to explicitly set the timezone when you're dealing with times. Yes, your server is in Los Angeles, but where are your users?

As explicitly dealing with timezones makes your application somewhat more complex (but also more correct, less surprising, harder to test) the next best would be to explicitly make tomcat (java) know what timezone your server clock is set to. Careful: There are some levels to set this: Set your server clock to UTC, configure your server OS to be PST and then let java know of the timezone that your server is on, e.g. in setenv.sh do CATALINA_OPTS="$CATALINA_OPTS -Duser.timezone=America/Los_Angeles" (or whatever your timezone is) to configure Java for your timezone.

Test, rinse, repeat until happy with the configuration. But make it an explicit choice on all different levels that you can set your hands on. Resolving the timezone is rather a java than a tomcat feature.

It's quite important for maintainability of your software to always store times in UTC. If you ever store in your local timezone, calculating any other timezone will be a mess - think daylight savings times, change of timezones of different areas of the world etc.

So: Set your server to UTC, then get the current time, check if it's correct. For display purposes, you might use the (user's) local timezone (e.g. PST), but for storage and calculation, UTC is highly recommended.

Change Timezone in Tomcat

Unless tomcat has a personal TZ environment variable, then it uses /etc/localtime which on my system is a copy of /usr/share/zoneinfo/America/New_York.

Tomcat's personal timezone would be specified in its startup script in a form like:

-Duser.timezone=GMT

Change time zone for tomcat


  1. Tomcat uses OS time settings.
  2. Best place is to set environment variables is setenv.bat (setenv.sh) in tomcat bin directory. Use set JAVA_OPTS="-Duser.timezone=GMT" for windows or export JAVA_OPTS="-Duser.timezone=GMT" for linux. Also make sure that catalina.sh contains script to call setenv file. It should be there by default.

How do I set the timezone in Tomcat for a single web app?

The only way I found is to setup a filter and change the timezone in the filter,

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
TimeZone savedZone = TimeZone.getDefault();
TimeZone.setDefault(webappZone);
chain.doFilter(request, response);
TimeZone.setDefault(savedZone);
}

The setDefault() changes the zone for the thread. So everything running in the thread inside the filter will have a different default timezone. We have to change it back because the thread is shared by other apps. You also need to do the same for your init(), destroy() methods and any other thread you might start in your application.

I had to do this because a third-party library assumes default timezone and we don't have source code. It was a mess because this changes log timezone but we don't want log in different times. The correct way to handle this is to use a specific timezone in any time value exposed to end users.

set timezone for tomcat

You should use the "rhc env set" command to set the JAVA_OPTS_EXT environment variable like this:

rhc env-set JAVA_OPTS_EXT="-Duser.timezone=Europe/Stockholm" --app <appname>

But I ultimately agree with Jon that you should avoid using that timezone at all.



Related Topics



Leave a reply



Submit