001: /*
002: *
003: *
004: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
005: * Reserved. Use is subject to license terms.
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License version
010: * 2 only, as published by the Free Software Foundation.
011: *
012: * This program is distributed in the hope that it will be useful, but
013: * WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License version 2 for more details (a copy is
016: * included at /legal/license.txt).
017: *
018: * You should have received a copy of the GNU General Public License
019: * version 2 along with this work; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
021: * 02110-1301 USA
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
024: * Clara, CA 95054 or visit www.sun.com if you need additional
025: * information or have any questions.
026: */
027:
028: /*
029: * (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved
030: * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
031: *
032: * The original version of this source code and documentation is copyrighted
033: * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
034: * materials are provided under terms of a License Agreement between Taligent
035: * and Sun. This technology is protected by multiple US and International
036: * patents. This notice and attribution to Taligent may not be removed.
037: * Taligent is a registered trademark of Taligent, Inc.
038: *
039: */
040:
041: package java.util;
042:
043: import com.sun.cldc.util.j2me.TimeZoneImpl;
044:
045: /**
046: * <code>TimeZone</code> represents a time zone offset, and also figures
047: * out daylight savings.
048: * <p>
049: * Typically, you get a <code>TimeZone</code> using <code>getDefault</code>
050: * which creates a <code>TimeZone</code> based on the time zone where the program
051: * is running. For example, for a program running in Japan, <code>getDefault</code>
052: * creates a <code>TimeZone</code> object based on Japanese Standard Time.
053: * <p>
054: * You can also get a <code>TimeZone</code> using <code>getTimeZone</code> along
055: * with a time zone ID. For instance, the time zone ID for the Pacific
056: * Standard Time zone is "PST". So, you can get a PST <code>TimeZone</code> object
057: * with:
058: * <blockquote>
059: * <pre>
060: * TimeZone tz = TimeZone.getTimeZone("PST");
061: * </pre>
062: * </blockquote>
063: *
064: * <p> This class is a pure subset of the java.util.TimeZone class in JDK 1.3.
065: * <p> The only time zone ID that is required to be supported is "GMT".
066: * <p>
067: * Apart from the methods and variables being subset, the semantics of the
068: * getTimeZone() method may also be subset: custom IDs such as "GMT-8:00"
069: * are not required to be supported.
070: *
071: * @see java.util.Calendar
072: * @see java.util.Date
073: * @version CLDC 1.1 02/01/2002 (Based on JDK 1.3)
074: */
075: public abstract class TimeZone {
076:
077: private static TimeZoneImpl defaultZone = null;
078: private static String platform = null;
079:
080: public TimeZone() {
081: }
082:
083: /**
084: * Gets offset, for current date, modified in case of
085: * daylight savings. This is the offset to add *to* GMT to get local time.
086: * Gets the time zone offset, for current date, modified in case of daylight
087: * savings. This is the offset to add *to* GMT to get local time. Assume
088: * that the start and end month are distinct. This method may return incorrect
089: * results for rules that start at the end of February (e.g., last Sunday in
090: * February) or the beginning of March (e.g., March 1).
091: *
092: * @param era The era of the given date (0 = BC, 1 = AD).
093: * @param year The year in the given date.
094: * @param month The month in the given date. Month is 0-based. e.g.,
095: * 0 for January.
096: * @param day The day-in-month of the given date.
097: * @param dayOfWeek The day-of-week of the given date.
098: * @param millis The milliseconds in day in <em>standard</em> local time.
099: * @return The offset to add *to* GMT to get local time.
100: * @exception IllegalArgumentException the era, month, day,
101: * dayOfWeek, or millis parameters are out of range
102: */
103: public abstract int getOffset(int era, int year, int month,
104: int day, int dayOfWeek, int millis);
105:
106: /**
107: * Gets the GMT offset for this time zone.
108: *
109: * @return the GMT offset for this time zone.
110: */
111: public abstract int getRawOffset();
112:
113: /**
114: * Queries if this time zone uses Daylight Savings Time.
115: *
116: * @return if this time zone uses Daylight Savings Time.
117: */
118: public abstract boolean useDaylightTime();
119:
120: /**
121: * Gets the ID of this time zone.
122: * @return the ID of this time zone.
123: */
124: public String getID() {
125: return null;
126: }
127:
128: /**
129: * Gets the <code>TimeZone</code> for the given ID.
130: * @param ID the ID for a <code>TimeZone</code>, either an abbreviation such as
131: * "GMT", or a full name such as "America/Los_Angeles".
132: * <p> The only time zone ID that is required to be supported is "GMT".
133: *
134: * @return the specified TimeZone, or the GMT zone if the given ID cannot be
135: * understood.
136: */
137: public static synchronized TimeZone getTimeZone(String ID) {
138: if (ID == null) {
139: throw new NullPointerException();
140: }
141: getDefault();
142: TimeZone tz = defaultZone.getInstance(ID);
143: if (tz == null) {
144: tz = defaultZone.getInstance("GMT");
145: }
146: return tz;
147: }
148:
149: /**
150: * Gets the default <code>TimeZone</code> for this host.
151: * The source of the default <code>TimeZone</code>
152: * may vary with implementation.
153: * @return a default <code>TimeZone</code>.
154: */
155:
156: /* <p>
157: * The following is information for implementers. Applications
158: * should not need to be aware of this or rely on it, because
159: * each implementation may do it differently:
160: * <p>
161: * The TimeZone class will look up a time zone implementation
162: * class at runtime. The class name will take the form:
163: * <p>
164: * <code>{classRoot}.util.{platform}.TimeZoneImpl</code>
165: * <p>
166: * To simplify things, we use a hard-coded path name here.
167: * Actual location of the implementation class may vary
168: * from one implementation to another.
169: */
170: public static synchronized TimeZone getDefault() {
171: if (defaultZone == null) {
172: try {
173: Class clazz = Class
174: .forName("com.sun.cldc.util.j2me.TimeZoneImpl");
175:
176: // Construct a new TimeZoneImpl instance
177: defaultZone = (TimeZoneImpl) clazz.newInstance();
178: defaultZone = (TimeZoneImpl) defaultZone
179: .getInstance(null);
180: } catch (Exception x) {
181: }
182: }
183: return defaultZone;
184: }
185:
186: /**
187: * Gets all the available IDs supported.
188: * @return an array of IDs.
189: */
190: public static String[] getAvailableIDs() {
191: getDefault();
192: return defaultZone.getIDs();
193: }
194: }
|