A patch to java.util.TimeZone

Ito Kazumitsu ito.kazumitsu at hitachi-cable.co.jp
Sun Nov 11 18:19:04 PST 2001


Attached is a patch to java.util.TimeZone which

  (1) avoids getTimeZone(ID) returning null when the ID cannot
      be understood.  In that case, getTimeZone(ID) return GMT. 
  (2) enables getTimeZone(ID) to understand custom ID such as
      "GMT+09:00".

--- TimeZone.java.orig	Fri Sep 22 22:01:54 2000
+++ TimeZone.java	Mon Nov 12 11:05:19 2001
@@ -134,7 +134,10 @@
 
 private static TimeZone loadTimeZone(String ID) {
 	Object tzo = zones.get(ID);
-	if (tzo == null || tzo instanceof TimeZone) {
+	if (tzo == null) {
+		return getCustomTimeZone(ID);
+        }
+	if (tzo instanceof TimeZone) {
 		return ((TimeZone)tzo);
 	}
 	if (!(tzo instanceof File)) {
@@ -153,6 +156,37 @@
 		tz = getTimeZone("GMT");
 		zones.put(ID, tz);
 	}
+	return (tz);
+}
+
+private static TimeZone getCustomTimeZone(String ID) {
+	TimeZone tz = getTimeZone("GMT");
+	if (ID.startsWith("GMT")) {
+		try {
+			String sign = ID.substring(3,4);
+			String hh;
+			String mm;
+			int i = ID.indexOf(":");
+			if (i >= 0) {
+				hh = ID.substring(4,i);
+				mm = ID.substring(i+1);
+			}
+			else {
+				hh = ID.substring(4,6);
+				mm = ID.substring(6,8);
+			}
+			int hhi = Integer.parseInt(hh);
+			int mmi = Integer.parseInt(mm);
+			int rawOffset = (hhi * 60 + mmi) * 60 * 1000;
+			if (sign.equals("+")) {
+				tz = new SimpleTimeZone(rawOffset, ID);
+			}
+			else if (sign.equals("-")) {
+				tz = new SimpleTimeZone(- rawOffset, ID);
+			}
+		} catch (Exception e) {}
+	}
+	zones.put(ID, tz);
 	return (tz);
 }
 


More information about the kaffe mailing list