View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  package de.bea.domingo.util;
23  
24  import java.io.ObjectStreamException;
25  import java.util.Calendar;
26  import java.util.Date;
27  import java.util.GregorianCalendar;
28  import java.util.TimeZone;
29  
30  /***
31   * A time-only Gregorian calendar.
32   *
33   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
34   */
35  public final class GregorianTimeZone extends GregorianCalendar {
36  
37      private static final long serialVersionUID = 1L;
38  
39      private static final int MILLIS_PER_HOUR = 3600000;
40  
41      /***
42       * Default Constructor.
43       */
44      public GregorianTimeZone() {
45          super();
46          computeFields();
47      }
48  
49      /***
50       * Creates a new Gregorian date from a given <code>java.util.Date</code>.
51       *
52       * @param date the date for the new calendar
53       */
54      public GregorianTimeZone(final Date date) {
55          super();
56          if (date != null) {
57              setTime(date);
58          }
59          computeFields();
60      }
61  
62      /***
63       * Creates a new Gregorian date from a given <code>java.util.Calendar</code>.
64       *
65       * @param calendar the original calendar for the new calendar
66       */
67      public GregorianTimeZone(final Calendar calendar) {
68          super();
69          if (calendar != null) {
70              set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
71              set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
72              set(Calendar.SECOND, calendar.get(Calendar.SECOND));
73              set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND));
74              computeFields();
75          }
76      }
77  
78      /***
79       * Creates a new Gregorian time zone from a given time zone.
80       *
81       * @param timezone the time zone
82       */
83      public GregorianTimeZone(final TimeZone timezone) {
84          super();
85          setTimeZone(timezone);
86          computeFields();
87      }
88  
89      /***
90       * Overwrite prevents setting date and time fields.
91       *
92       * @param field the given calendar field.
93       * @param value the value to be set for the given calendar field.
94       *
95       * @see java.util.Calendar#set(int, int)
96       */
97      public void set(final int field, final int value) {
98      }
99  
100     /***
101      * Formats a date as a string in a locale-independent format.
102      *
103      * @return string representation of date in reverse format with leading
104      *         zeros: <code>YYYY-MM-DD</code>.
105      *
106      * @see java.lang.Object#toString()
107      */
108     public String toString() {
109         // FIXME getDSTSavings() should be compatible with JDK 1.3
110         return getTimeZone().getID() + "/" + (getTimeZone().getDSTSavings() / MILLIS_PER_HOUR) + "h";
111     }
112 
113     /***
114      * Overwrites GregorianCalendar.computeFields(), to disable all date and
115      * time fields.
116      *
117      * @see java.util.Calendar#computeFields()
118      */
119     protected void computeFields() {
120         super.computeFields();
121         clear(Calendar.ERA);
122         clear(Calendar.YEAR);
123         clear(Calendar.MONTH);
124         clear(Calendar.WEEK_OF_YEAR);
125         clear(Calendar.WEEK_OF_MONTH);
126         clear(Calendar.DAY_OF_MONTH);
127         clear(Calendar.DAY_OF_YEAR);
128         clear(Calendar.DAY_OF_WEEK);
129         clear(Calendar.DAY_OF_WEEK_IN_MONTH);
130         clear(Calendar.HOUR_OF_DAY);
131         clear(Calendar.HOUR);
132         clear(Calendar.MINUTE);
133         clear(Calendar.SECOND);
134         clear(Calendar.AM_PM);
135         clear(Calendar.MILLISECOND);
136     }
137 
138     /***
139      * Serialization helper. Usually used to resolve singletons or other static
140      * instances. Here used to compute all fields, especially the array
141      * <code>stamp</code> which is not serialized in <code>JDK 1.3.1</code>.
142      */
143     private Object readResolve() throws ObjectStreamException {
144         computeFields();
145         return this;
146     }
147 
148     /***
149      * Indicates whether some other object is "equal to" this one.
150      *
151      * @param object the reference object with which to compare.
152      * @return <code>true</code> if this object is the same as the object
153      *         argument; <code>false</code> otherwise.
154      *
155      * @see java.lang.Object#equals(java.lang.Object)
156      */
157     public boolean equals(final Object object) {
158         if (this == object) {
159             return true;
160         }
161         if (!(object instanceof java.util.Calendar)) {
162             return false;
163         } else {
164             TimeZone tz1 = ((java.util.Calendar) object).getTimeZone();
165             TimeZone tz2 = getTimeZone();
166             // todo write unit tests for the equals method
167             return tz1.getID().equals(tz2.getID());
168         }
169     }
170 
171     /***
172      * Returns a hash code value for the object.
173      *
174      * @return a hash code value for this object.
175      *
176      * @see java.lang.Object#hashCode()
177      */
178     public int hashCode() {
179         return get(Calendar.HOUR) << (2 * 2 * 3) | get(Calendar.MINUTE) << (2 * 3) | get(Calendar.SECOND);
180     }
181 }