001: /*
002: * Copyright (C) 1999-2005 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.lib.date;
019:
020: import java.util.GregorianCalendar;
021: import java.util.Calendar;
022: import java.util.TimeZone;
023: import java.util.Locale;
024:
025: /**
026: * A <code>SimpleDate</code> implementation which extends the <code>java.util.GregorianCalendar</code>.
027: * A <code>SimpleDate</code> can be in synchronized mode and not synchronized (absolute) mode.
028: * In the not synchronized mode the absolute time values independent from the time zone are used for
029: * comparison of calendar objects (e.g. a SimpleDate with time value 19:00 p.m. in Germany Berlin is
030: * the same as SimpleDate object with time value 19:00 p.m. in USA Los Angeles).
031: * In the synchronized mode two SimpleDate objects are equal if they have the same real time value
032: * (e.g. a SimpleDate with time value 19:00 p.m. in Germany Berlin is the same as the SimpleDate object with
033: * time value 10:00 a.m. in USA Los Angeles (- 8 hours)). The time zone offset is given by the java.util.Timezone.
034: *
035: *
036: * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
037: * @version 3.4 <7 March 05>
038: * @since 3.3.1
039: */
040: public class SimpleDate extends GregorianCalendar {
041:
042: private boolean syn = true;
043:
044: ///////////////
045: // Constructors
046: ///////////////
047:
048: /**
049: * Constructs a default, synchronized <code>SimpleDate</code> using the current time
050: * in the default time zone with the default locale.
051: */
052: public SimpleDate() {
053: super ();
054: this .syn = true;
055: }
056:
057: /**
058: * Constructs a default<code>SimpleDate</code> using the current time
059: * in the default time zone with the default locale and synchronized flag.
060: */
061: public SimpleDate(boolean syn) {
062: super ();
063: this .syn = syn;
064: }
065:
066: /**
067: * Constructs a <code>SimpleDate</code> based on the current time
068: * in the given time zone with the default locale.
069: *
070: * @param zone the given time zone.
071: * @param syn synchronized flag
072: */
073: public SimpleDate(TimeZone zone, boolean syn) {
074: super (zone);
075: this .syn = syn;
076: }
077:
078: /**
079: * Constructs a <code>SimpleDate</code> based on the current time
080: * in the default time zone with the given locale and synchronized flag.
081: *
082: * @param aLocale the given locale.
083: * @param syn synchronized flag
084: */
085: public SimpleDate(Locale aLocale, boolean syn) {
086: super (aLocale);
087: this .syn = syn;
088: }
089:
090: /**
091: * Constructs a <code>SimpleDate</code> based on the current time
092: * in the given time zone with the given locale and synchronized flag.
093: *
094: * @param zone the given time zone.
095: * @param aLocale the given locale.
096: * @param syn synchronized flag
097: */
098: public SimpleDate(TimeZone zone, Locale aLocale, boolean syn) {
099: super (zone, aLocale);
100: this .syn = syn;
101: }
102:
103: /**
104: * Constructs a <code>SimpleDate</code> with the given date set
105: * in the default time zone with the default locale and synchronized flag.
106: *
107: * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
108: * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
109: * Month value is 0-based. e.g., 0 for January.
110: * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
111: * @param syn synchronized flag
112: */
113: public SimpleDate(int year, int month, int dayOfMonth, boolean syn) {
114: super (year, month, dayOfMonth);
115: this .syn = syn;
116: }
117:
118: /**
119: * Constructs a <code>SimpleDate</code> with the given date
120: * and time set for the default time zone with the default locale and synchronized flag.
121: *
122: * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
123: * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
124: * Month value is 0-based. e.g., 0 for January.
125: * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
126: * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
127: * in the calendar.
128: * @param minute the value used to set the <code>MINUTE</code> calendar field
129: * in the calendar.
130: */
131: public SimpleDate(int year, int month, int dayOfMonth,
132: int hourOfDay, int minute, boolean syn) {
133: super (year, month, dayOfMonth, hourOfDay, minute);
134: this .syn = syn;
135: }
136:
137: /**
138: * Constructs a SimpleDate with the given date
139: * and time set for the default time zone with the default locale and synchronized flag.
140: *
141: * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
142: * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
143: * Month value is 0-based. e.g., 0 for January.
144: * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
145: * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
146: * in the calendar.
147: * @param minute the value used to set the <code>MINUTE</code> calendar field
148: * in the calendar.
149: * @param second the value used to set the <code>SECOND</code> calendar field
150: * in the calendar.
151: */
152: public SimpleDate(int year, int month, int dayOfMonth,
153: int hourOfDay, int minute, int second, boolean syn) {
154: super (year, month, dayOfMonth, hourOfDay, minute, second);
155: this .syn = syn;
156: }
157:
158: /**
159: * Constructs a synchronized <code>SimpleDate</code> based on the current time
160: * in the given time zone with the default locale.
161: *
162: * @param zone the given time zone.
163: */
164: public SimpleDate(TimeZone zone) {
165: super (zone);
166: this .syn = true;
167: }
168:
169: /**
170: * Constructs a synchronized <code>SimpleDate</code> based on the current time
171: * in the default time zone with the given locale.
172: *
173: * @param aLocale the given locale.
174: */
175: public SimpleDate(Locale aLocale) {
176: super (aLocale);
177: this .syn = true;
178: }
179:
180: /**
181: * Constructs a synchronized<code>SimpleDate</code> based on the current time
182: * in the given time zone with the given locale.
183: *
184: * @param zone the given time zone.
185: * @param aLocale the given locale.
186: */
187: public SimpleDate(TimeZone zone, Locale aLocale) {
188: super (zone, aLocale);
189: this .syn = true;
190: }
191:
192: /**
193: * Constructs a synchronized<code>SimpleDate</code> with the given date set
194: * in the default time zone with the default locale.
195: *
196: * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
197: * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
198: * Month value is 0-based. e.g., 0 for January.
199: * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
200: */
201: public SimpleDate(int year, int month, int dayOfMonth) {
202: super (year, month, dayOfMonth);
203: this .syn = true;
204: }
205:
206: /**
207: * Constructs a synchronized<code>SimpleDate</code> with the given date
208: * and time set for the default time zone with the default locale.
209: *
210: * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
211: * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
212: * Month value is 0-based. e.g., 0 for January.
213: * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
214: * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
215: * in the calendar.
216: * @param minute the value used to set the <code>MINUTE</code> calendar field
217: * in the calendar.
218: */
219: public SimpleDate(int year, int month, int dayOfMonth,
220: int hourOfDay, int minute) {
221: super (year, month, dayOfMonth, hourOfDay, minute);
222: this .syn = true;
223: }
224:
225: /**
226: * Constructs a synchronized SimpleDate with the given date
227: * and time set for the default time zone with the default locale.
228: *
229: * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
230: * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
231: * Month value is 0-based. e.g., 0 for January.
232: * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
233: * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
234: * in the calendar.
235: * @param minute the value used to set the <code>MINUTE</code> calendar field
236: * in the calendar.
237: * @param second the value used to set the <code>SECOND</code> calendar field
238: * in the calendar.
239: */
240: public SimpleDate(int year, int month, int dayOfMonth,
241: int hourOfDay, int minute, int second) {
242: super (year, month, dayOfMonth, hourOfDay, minute, second);
243: this .syn = true;
244: }
245:
246: /////////////////
247: // Public methods
248: /////////////////
249:
250: /**
251: * Indicated wether this SimpleDate is in synchronized mode or not
252: * @return boolean true:synchronized - false: not synchronize
253: */
254: public boolean isSynchronized() {
255: return syn;
256: }
257:
258: /**
259: * Set the synchronized mode
260: * @param syn synchronized flag
261: */
262: public void setSynchronized(boolean syn) {
263: this .syn = syn;
264: }
265:
266: /**
267: * Compares this <code>SimpleDate</code> to the specified
268: * <code>Object</code>. The result is <code>true</code> if and
269: * only if the argument is a instance of <code>Calendar</code>
270: * that represents the same time value (millisecond offset from
271: * the <a href="Calendar.html#Epoch">Epoch</a>) in the synchronized
272: * mode or the time value + the time zone offsets (in milliseconds) as
273: * this object (~ the absolute times are compared).
274: *
275: * @param obj the object to compare with.
276: * @return <code>true</code> if this object is equal to <code>obj</code>;
277: * <code>false</code> otherwise.
278: */
279: public boolean equals(Object obj) {
280: if ((obj instanceof SimpleDate) || (obj instanceof Calendar)) {
281: // compare absolute times (GMT time in milliseconds + zone offset in milliseconds)
282: if (!this .syn)
283: return this .getTimeInMillis()
284: + this .get(Calendar.ZONE_OFFSET) == ((Calendar) obj)
285: .getTimeInMillis()
286: + ((Calendar) obj).get(Calendar.ZONE_OFFSET);
287: if (obj instanceof SimpleDate)
288: if (!((SimpleDate) obj).isSynchronized())
289: return this .getTimeInMillis()
290: + this .get(Calendar.ZONE_OFFSET) == ((Calendar) obj)
291: .getTimeInMillis()
292: + ((Calendar) obj)
293: .get(Calendar.ZONE_OFFSET);
294: // compare synchronized times (GMT time in milliseconds)
295: return this .getTimeInMillis() == ((Calendar) obj)
296: .getTimeInMillis();
297: }
298: return false;
299: }
300:
301: /**
302: * Returns whether this <code>SimpleDate</code> represents a time
303: * before the time represented by the specified
304: * <code>Object</code>, if and only if <code>obj</code> is a <code>Calendar</code>
305: * instance. Otherwise, the method returns <code>false</code>.
306: * In synchronized mode only the time values are compared, in not synchronized mode
307: * the time zone offsets are added (~ the absolute times are compared).
308: *
309: * @param obj the <code>Object</code> to be compared
310: * @return <code>true</code> if the time of this
311: * <code>Calendar</code> is before the time represented by
312: * <code>obj</code>; <code>false</code> otherwise.
313: */
314: public boolean before(Object obj) {
315: if ((obj instanceof SimpleDate) || (obj instanceof Calendar)) {
316: // compare absolute times (GMT time in milliseconds + zone offset in milliseconds)
317: if (!this .syn)
318: return this .getTimeInMillis()
319: + this .get(Calendar.ZONE_OFFSET) < ((Calendar) obj)
320: .getTimeInMillis()
321: + ((Calendar) obj).get(Calendar.ZONE_OFFSET);
322: if (obj instanceof SimpleDate)
323: if (!((SimpleDate) obj).isSynchronized())
324: return this .getTimeInMillis()
325: + this .get(Calendar.ZONE_OFFSET) < ((Calendar) obj)
326: .getTimeInMillis()
327: + ((Calendar) obj)
328: .get(Calendar.ZONE_OFFSET);
329: // compare synchronized times (GMT time in milliseconds)
330: return this .getTimeInMillis() < ((Calendar) obj)
331: .getTimeInMillis();
332: }
333: return false;
334: }
335:
336: /**
337: * Returns whether this <code>SimpleDate</code> represents a time
338: * after the time represented by the specified
339: * <code>Object</code>, if and only if <code>obj</code> is a <code>Calendar</code>
340: * instance. Otherwise, the method returns <code>false</code>.
341: * In synchronized mode only the time values are compared, in not synchronized mode
342: * the time zone offsets are added (~the absolute times are compared).
343: *
344: * @param obj the <code>Object</code> to be compared
345: * @return <code>true</code> if the time of this <code>Calendar</code> is
346: * after the time represented by <code>when</code>; <code>false</code>
347: * otherwise.
348: * @see #compareTo(Calendar)
349: */
350: public boolean after(Object obj) {
351: if ((obj instanceof SimpleDate) || (obj instanceof Calendar)) {
352: // compare absolute times (GMT time in milliseconds + zone offset in milliseconds)
353: if (!this .syn)
354: return this .getTimeInMillis()
355: + this .get(Calendar.ZONE_OFFSET) > ((Calendar) obj)
356: .getTimeInMillis()
357: + ((Calendar) obj).get(Calendar.ZONE_OFFSET);
358: if (obj instanceof SimpleDate)
359: if (!((SimpleDate) obj).isSynchronized())
360: return this .getTimeInMillis()
361: + this .get(Calendar.ZONE_OFFSET) > ((Calendar) obj)
362: .getTimeInMillis()
363: + ((Calendar) obj)
364: .get(Calendar.ZONE_OFFSET);
365: // compare synchronized times (GMT time in milliseconds)
366: return this .getTimeInMillis() > ((Calendar) obj)
367: .getTimeInMillis();
368: }
369: return false;
370: }
371:
372: }
|