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.

TimeZoneServlet.java

package com.splike.timezoneservlet;

import java.io.IOException;
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 {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream out = response.getOutputStream();
		
		response.setContentType("text/html");
		
		out.println("<a href=\"?\">refresh view w/o changing settings</a><br/>");
		out.println();
		
		String tzParam = request.getParameter("tz");
		if (tzParam != null) {
			if (tzParam.equals("")) {
				TimeZone.setDefault(null);
				out.println("Action taken: Reset default TZ");
			} else {
				TimeZone.setDefault(TimeZone.getTimeZone(tzParam));
				out.println("Action taken: Set default TZ to " + tzParam);
			}
		}
		
		out.println("<pre>");
		out.println("Current Date: " + new Date());
		out.println("Default TZ: " + TimeZone.getDefault().getID() + " (" + TimeZone.getDefault().getDisplayName() + ")");
		out.println();
		out.println();
		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();
				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();
		}
		out.println("</pre>");
	}

}

web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	<display-name>Time Zone Servlet</display-name>
	<description>Time Zone Servlet</description>


	<servlet>
		<servlet-name>TimeZoneServlet</servlet-name>
		<servlet-class>com.splike.TimeZoneServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>TimeZoneServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

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

http://www.apache.org/licenses/LICENSE-2.0

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]
thanks!
Permalink 02/09/06 @ 21:54
Comment from: Stian [Visitor]
Excellent!

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>
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)

<  September 2019  >
Mon Tue Wed Thu Fri Sat Sun
            1
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
30

Categories


Archives

Misc

Syndicate this blog XML

powered by
b2evolution