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.util.Calendar;
25  import java.util.Date;
26  import java.util.GregorianCalendar;
27  import java.util.TimeZone;
28  
29  /***
30   * A time-only Gregorian calendar.
31   * Milliseconds are always zero,
32   *
33   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
34   */
35  public final class GregorianTime extends GregorianCalendar {
36  
37      /*** serial version ID for serialization. */
38      private static final long serialVersionUID = 1L;
39  
40      /*** Default time zone: Greenwich meantime. */
41      private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
42  
43      /***
44       * Default Constructor.
45       */
46      public GregorianTime() {
47          super(GMT);
48          computeTime();
49      }
50  
51      /***
52       * Creates a new Gregorian date from a given <code>java.util.Date</code>.
53       *
54       * @param date the date for the new calendar
55       */
56      public GregorianTime(final Date date) {
57          super(GMT);
58          if (date != null) {
59              setTime(date);
60          }
61          computeTime();
62      }
63  
64      /***
65       * Creates a new Gregorian date from a given <code>java.util.Calendar</code>.
66       *
67       * @param calendar the original calendar for the new calendar
68       */
69      public GregorianTime(final Calendar calendar) {
70          super(GMT);
71          if (calendar != null) {
72              setTime(calendar.getTime());
73          }
74          computeTime();
75      }
76  
77      /***
78       * Creates a new Gregorian date from given year, month and date.
79       *
80       * <p>The first month of the year is <code>JANUARY</code> which is 0; the
81       * last month is <code>DEDCEMBER</code> which is 11.</p>
82       *
83       * @param hour the hour of the new calendar
84       * @param minute the minute of the new calendar
85       * @param second the second of the new calendar
86       */
87      public GregorianTime(final int hour, final int minute, final int second) {
88          super(GMT);
89          set(Calendar.HOUR_OF_DAY, hour);
90          set(Calendar.MINUTE, minute);
91          set(Calendar.SECOND, second);
92          computeTime();
93      }
94  
95      /***
96       * Overwrite prevents setting date fields.
97       *
98       * @param field the given calendar field.
99       * @param value the value to be set for the given calendar field.
100      *
101      * @see java.util.Calendar#set(int, int)
102      */
103     public void set(final int field, final int value) {
104         if (isDateField(field)) {
105             return;
106         }
107         super.set(field, value);
108     }
109 
110     /***
111      * Overwrite to prevent setting date fields.
112      *
113      * @param field the time field.
114      * @param amount the amount of date or time to be added to the field.
115      *
116      * @see java.util.GregorianCalendar#add(int, int)
117      */
118     public void add(final int field, final int amount) {
119         if (isDateField(field)) {
120             return;
121         }
122         super.add(field, amount);
123         computeTime();
124     }
125 
126     /***
127      * Checks if a given field number indicating a time field.
128      * @param field field index
129      * @return <code>true</code> if the field number indicating a time field, else <code>false</code>
130      */
131     private boolean isDateField(final int field) {
132         if (field == Calendar.ERA || field == Calendar.YEAR) {
133             return true;
134         }
135         if (field == Calendar.MONTH || field == Calendar.DATE) {
136             return true;
137         }
138         if (field == Calendar.WEEK_OF_MONTH || field == Calendar.WEEK_OF_YEAR) {
139             return true;
140         }
141         if (field == Calendar.MILLISECOND) {
142             return true;
143         }
144         return false;
145     }
146 
147     /***
148      * Overwrite prevents setting a time zone to keep the time unchanged and the date unavailable.
149      *
150      * @param zone the new time zone (ignored)
151      *
152      * @see java.util.Calendar#setTimeZone(java.util.TimeZone)
153      */
154     public void setTimeZone(final TimeZone zone) {
155     }
156 
157     /***
158      * Overwrites {@link Calendar#setTimeInMillis(long)}, to disable all date fields.
159      *
160      * @param millis the new time in UTC milliseconds from the epoch.
161      * @see java.util.Calendar#setTimeInMillis(long)
162      */
163     public void setTimeInMillis(final long millis) {
164         super.setTimeInMillis(millis);
165         computeTime();
166     }
167 
168     /***
169      * Overwrites GregorianCalendar.computeFields(), to disable all date fields.
170      *
171      * @see java.util.Calendar#computeTime()
172      */
173     protected void computeFields() {
174         super.computeFields();
175         computeTime();
176     }
177 
178     /***
179      * Overwrites GregorianCalendar.computeTime(), to disable all date fields.
180      *
181      * @see java.util.Calendar#computeTime()
182      */
183     protected void computeTime() {
184         clearFields();
185         super.computeTime();
186     }
187 
188     /***
189      * Clears all date fields and the millis.
190      */
191     private void clearFields() {
192         clear(Calendar.ERA);
193         clear(Calendar.YEAR);
194         clear(Calendar.MONTH);
195         clear(Calendar.WEEK_OF_YEAR);
196         clear(Calendar.WEEK_OF_MONTH);
197         clear(Calendar.DAY_OF_MONTH);
198         clear(Calendar.DAY_OF_YEAR);
199         clear(Calendar.DAY_OF_WEEK);
200         clear(Calendar.DAY_OF_WEEK_IN_MONTH);
201         clear(Calendar.MILLISECOND);
202     }
203 
204     /***
205      * Returns the hour of the calendar.
206      *
207      * @return the hour
208      */
209     public int getHour() {
210         return get(Calendar.HOUR_OF_DAY);
211     }
212 
213     /***
214      * Returns the minute of the calendar.
215      *
216      * @return the minute
217      */
218     public int getMinute() {
219         return get(Calendar.MINUTE);
220     }
221 
222     /***
223      * Returns the second of the calendar.
224      *
225      * @return the second
226      */
227     public int getSecond() {
228         return get(Calendar.SECOND);
229     }
230 
231     /***
232      * {@inheritDoc}
233      *
234      * @see java.util.Calendar#toString()
235      */
236     public String toString() {
237         return DateUtil.getTimeString(this);
238     }
239 }