Post details: Timezones in Tomcat

Feb 07, 2006 : Timezones in Tomcat

Update Feb 8, 2005: Java system property user.timezone works. Java 1.5 before update 7 has a bug so default timezone is per-thread instead of per VM, which complicated things. And worse, I was using another webapp that set user.timezone to GMT all the time.

As far as I can tell, there is no guaranteed way to change the timezone of Java at runtime (except by code that executes TimeZone.setDefault()). Property -Duser.timezone=?? and env var TZ=?? may or may not work. And for me, at least, a standard Tomcat 5.5 installation in Windows has a default timezone of GMT, despite any OS settings.

So I wrote a servlet to help. Changes seem to persist through Tomcat restarts. Make sure you put some security on it.

package com.splike.timezoneservlet;

import java.util.Date;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TimeZoneServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream out = response.getOutputStream();
		out.println("<a href=\"?\">refresh view w/o changing settings</a><br/>");
		String tzParam = request.getParameter("tz");
		if (tzParam != null) {
			if (tzParam.equals("")) {
				out.println("Action taken: Reset default TZ");
			} else {
				out.println("Action taken: Set default TZ to " + tzParam);
		out.println("Current Date: " + new Date());
		out.println("Default TZ: " + TimeZone.getDefault().getID() + " (" + TimeZone.getDefault().getDisplayName() + ")");
		out.println("Set the default TZ:");
		out.println("<a href=\"?tz=\">reset</a>");
		int last_offset = -1;
		for (String tzId : TimeZone.getAvailableIDs()) {
			TimeZone tz = TimeZone.getTimeZone(tzId);
			if (last_offset != tz.getRawOffset()) {
				out.println("<b>Offset " + (float)tz.getRawOffset()/(1000*60*60) + ":</b>");
			out.println("<a href=\"?tz=" + tzId + "\">" + tzId + "</a> (" + tz.getDisplayName() + ")");
			last_offset = tz.getRawOffset();



<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns=""
	<display-name>Time Zone Servlet</display-name>
	<description>Time Zone Servlet</description>



Copyright 2006 Dave Brondsema, Cornerstone University

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Comments, Pingbacks:

Comment from: dad [Visitor]
Permalink 02/09/06 @ 21:54
Comment from: Stian [Visitor]

Even though this article is 3 years old, it still helped me out. :-)
Permalink 05/11/09 @ 09:32

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.
What is the 2-letter abbreviation for Michigan?
Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email & url)

<  September 2019  >
Mon Tue Wed Thu Fri Sat Sun
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29




Syndicate this blog XML

powered by