KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > Day


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * --------
27  * Day.java
28  * --------
29  * (C) Copyright 2001-2004, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: Day.java,v 1.7 2005/05/19 10:35:27 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 11-Oct-2001 : Version 1 (DG);
39  * 15-Nov-2001 : Updated Javadoc comments (DG);
40  * 04-Dec-2001 : Added static method to parse a string into a Day object (DG);
41  * 19-Dec-2001 : Added new constructor as suggested by Paul English (DG);
42  * 29-Jan-2002 : Changed getDay() method to getSerialDate() (DG);
43  * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to
44  * evaluate with reference to a particular time zone (DG);
45  * 19-Mar-2002 : Changed the API for the TimePeriod classes (DG);
46  * 29-May-2002 : Fixed bug in equals method (DG);
47  * 24-Jun-2002 : Removed unnecessary imports (DG);
48  * 10-Sep-2002 : Added getSerialIndex() method (DG);
49  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
50  * 10-Jan-2003 : Changed base class and method names (DG);
51  * 13-Mar-2003 : Moved to com.jrefinery.data.time package, and implemented
52  * Serializable (DG);
53  * 21-Oct-2003 : Added hashCode() method (DG);
54  * 30-Sep-2004 : Replaced getTime().getTime() with getTimeInMillis() (DG);
55  * 04-Nov-2004 : Reverted change of 30-Sep-2004, because it won't work for
56  * JDK 1.3 (DG);
57  *
58  */

59
60 package org.jfree.data.time;
61
62 import java.io.Serializable JavaDoc;
63 import java.text.DateFormat JavaDoc;
64 import java.text.ParseException JavaDoc;
65 import java.text.SimpleDateFormat JavaDoc;
66 import java.util.Calendar JavaDoc;
67 import java.util.Date JavaDoc;
68 import java.util.TimeZone JavaDoc;
69
70 import org.jfree.date.SerialDate;
71
72 /**
73  * Represents a single day in the range 1-Jan-1900 to 31-Dec-9999. This class
74  * is immutable, which is a requirement for all {@link RegularTimePeriod}
75  * subclasses.
76  */

77 public class Day extends RegularTimePeriod implements Serializable JavaDoc {
78
79     /** For serialization. */
80     private static final long serialVersionUID = -7082667380758962755L;
81     
82     /** A standard date formatter. */
83     protected static final DateFormat JavaDoc DATE_FORMAT
84         = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
85
86     /** A date formatter for the default locale. */
87     protected static final DateFormat JavaDoc
88         DATE_FORMAT_SHORT = DateFormat.getDateInstance(DateFormat.SHORT);
89
90     /** A date formatter for the default locale. */
91     protected static final DateFormat JavaDoc
92         DATE_FORMAT_MEDIUM = DateFormat.getDateInstance(DateFormat.MEDIUM);
93
94     /** A date formatter for the default locale. */
95     protected static final DateFormat JavaDoc
96         DATE_FORMAT_LONG = DateFormat.getDateInstance(DateFormat.LONG);
97
98     /** The day (uses SerialDate for convenience). */
99     private SerialDate serialDate;
100
101     /**
102      * Creates a new instance, derived from the system date/time (and assuming
103      * the default timezone).
104      */

105     public Day() {
106         this(new Date JavaDoc());
107     }
108
109     /**
110      * Constructs a new one day time period.
111      *
112      * @param day the day-of-the-month.
113      * @param month the month (1 to 12).
114      * @param year the year (1900 <= year <= 9999).
115      */

116     public Day(int day, int month, int year) {
117         this.serialDate = SerialDate.createInstance(day, month, year);
118     }
119
120     /**
121      * Constructs a new one day time period.
122      *
123      * @param serialDate the day (<code>null</code> not permitted).
124      */

125     public Day(SerialDate serialDate) {
126         if (serialDate == null) {
127             throw new IllegalArgumentException JavaDoc("Null 'serialDate' argument.");
128         }
129         this.serialDate = serialDate;
130     }
131
132     /**
133      * Constructs a new instance, based on a particular date/time and the
134      * default time zone.
135      *
136      * @param time the time (<code>null</code> not permitted).
137      */

138     public Day(Date JavaDoc time) {
139         // defer argument checking...
140
this(time, RegularTimePeriod.DEFAULT_TIME_ZONE);
141     }
142
143     /**
144      * Constructs a new instance, based on a particular date/time and time zone.
145      *
146      * @param time the date/time.
147      * @param zone the time zone.
148      */

149     public Day(Date JavaDoc time, TimeZone JavaDoc zone) {
150         if (time == null) {
151             throw new IllegalArgumentException JavaDoc("Null 'time' argument.");
152         }
153         if (zone == null) {
154             throw new IllegalArgumentException JavaDoc("Null 'zone' argument.");
155         }
156         Calendar JavaDoc calendar = Calendar.getInstance(zone);
157         calendar.setTime(time);
158         int d = calendar.get(Calendar.DAY_OF_MONTH);
159         int m = calendar.get(Calendar.MONTH) + 1;
160         int y = calendar.get(Calendar.YEAR);
161         this.serialDate = SerialDate.createInstance(d, m, y);
162     }
163
164     /**
165      * Returns the day as a {@link SerialDate}. Note: the reference that is
166      * returned should be an instance of an immutable {@link SerialDate}
167      * (otherwise the caller could use the reference to alter the state of
168      * this <code>Day</code> instance, and <code>Day</code> is supposed
169      * to be immutable).
170      *
171      * @return The day as a {@link SerialDate}.
172      */

173     public SerialDate getSerialDate() {
174         return this.serialDate;
175     }
176
177     /**
178      * Returns the year.
179      *
180      * @return The year.
181      */

182     public int getYear() {
183         return this.serialDate.getYYYY();
184     }
185
186     /**
187      * Returns the month.
188      *
189      * @return The month.
190      */

191     public int getMonth() {
192         return this.serialDate.getMonth();
193     }
194
195     /**
196      * Returns the day of the month.
197      *
198      * @return The day of the month.
199      */

200     public int getDayOfMonth() {
201         return this.serialDate.getDayOfMonth();
202     }
203
204     /**
205      * Returns the day preceding this one.
206      *
207      * @return The day preceding this one.
208      */

209     public RegularTimePeriod previous() {
210
211         Day result;
212         int serial = this.serialDate.toSerial();
213         if (serial > SerialDate.SERIAL_LOWER_BOUND) {
214             SerialDate yesterday = SerialDate.createInstance(serial - 1);
215             return new Day(yesterday);
216         }
217         else {
218             result = null;
219         }
220         return result;
221
222     }
223
224     /**
225      * Returns the day following this one, or <code>null</code> if some limit
226      * has been reached.
227      *
228      * @return The day following this one, or <code>null</code> if some limit
229      * has been reached.
230      */

231     public RegularTimePeriod next() {
232
233         Day result;
234         int serial = this.serialDate.toSerial();
235         if (serial < SerialDate.SERIAL_UPPER_BOUND) {
236             SerialDate tomorrow = SerialDate.createInstance(serial + 1);
237             return new Day(tomorrow);
238         }
239         else {
240             result = null;
241         }
242         return result;
243
244     }
245
246     /**
247      * Returns a serial index number for the day.
248      *
249      * @return The serial index number.
250      */

251     public long getSerialIndex() {
252         return this.serialDate.toSerial();
253     }
254
255     /**
256      * Returns the first millisecond of the day, evaluated using the supplied
257      * calendar (which determines the time zone).
258      *
259      * @param calendar calendar to use.
260      *
261      * @return The start of the day as milliseconds since 01-01-1970.
262      */

263     public long getFirstMillisecond(Calendar JavaDoc calendar) {
264
265         int year = this.serialDate.getYYYY();
266         int month = this.serialDate.getMonth();
267         int day = this.serialDate.getDayOfMonth();
268         calendar.clear();
269         calendar.set(year, month - 1, day, 0, 0, 0);
270         calendar.set(Calendar.MILLISECOND, 0);
271         //return calendar.getTimeInMillis(); // this won't work for JDK 1.3
272
return calendar.getTime().getTime();
273
274     }
275
276     /**
277      * Returns the last millisecond of the day, evaluated using the supplied
278      * calendar (which determines the time zone).
279      *
280      * @param calendar calendar to use.
281      *
282      * @return The end of the day as milliseconds since 01-01-1970.
283      */

284     public long getLastMillisecond(Calendar JavaDoc calendar) {
285
286         int year = this.serialDate.getYYYY();
287         int month = this.serialDate.getMonth();
288         int day = this.serialDate.getDayOfMonth();
289         calendar.clear();
290         calendar.set(year, month - 1, day, 23, 59, 59);
291         calendar.set(Calendar.MILLISECOND, 999);
292         //return calendar.getTimeInMillis(); // this won't work for JDK 1.3
293
return calendar.getTime().getTime();
294
295     }
296
297     /**
298      * Tests the equality of this Day object to an arbitrary object. Returns
299      * true if the target is a Day instance or a SerialDate instance
300      * representing the same day as this object. In all other cases,
301      * returns false.
302      *
303      * @param obj the object.
304      *
305      * @return A flag indicating whether or not an object is equal to this day.
306      */

307     public boolean equals(Object JavaDoc obj) {
308         
309         if (obj == this) {
310             return true;
311         }
312         if (!(obj instanceof Day)) {
313             return false;
314         }
315         Day that = (Day) obj;
316         if (!this.serialDate.equals(that.getSerialDate())) {
317             return false;
318         }
319         return true;
320         
321     }
322
323     /**
324      * Returns a hash code for this object instance. The approach described by
325      * Joshua Bloch in "Effective Java" has been used here:
326      * <p>
327      * <code>http://developer.java.sun.com/developer/Books/effectivejava
328      * /Chapter3.pdf</code>
329      *
330      * @return A hash code.
331      */

332     public int hashCode() {
333         return this.serialDate.hashCode();
334     }
335
336     /**
337      * Returns an integer indicating the order of this Day object relative to
338      * the specified object:
339      *
340      * negative == before, zero == same, positive == after.
341      *
342      * @param o1 the object to compare.
343      *
344      * @return negative == before, zero == same, positive == after.
345      */

346     public int compareTo(Object JavaDoc o1) {
347
348         int result;
349
350         // CASE 1 : Comparing to another Day object
351
// ----------------------------------------
352
if (o1 instanceof Day) {
353             Day d = (Day) o1;
354             result = -d.getSerialDate().compare(this.serialDate);
355         }
356
357         // CASE 2 : Comparing to another TimePeriod object
358
// -----------------------------------------------
359
else if (o1 instanceof RegularTimePeriod) {
360             // more difficult case - evaluate later...
361
result = 0;
362         }
363
364         // CASE 3 : Comparing to a non-TimePeriod object
365
// ---------------------------------------------
366
else {
367             // consider time periods to be ordered after general objects
368
result = 1;
369         }
370
371         return result;
372
373     }
374
375     /**
376      * Returns a string representing the day.
377      *
378      * @return A string representing the day.
379      */

380     public String JavaDoc toString() {
381         return this.serialDate.toString();
382     }
383
384     /**
385      * Parses the string argument as a day.
386      * <P>
387      * This method is required to recognise YYYY-MM-DD as a valid format.
388      * Anything else, for now, is a bonus.
389      *
390      * @param s the date string to parse.
391      *
392      * @return <code>null</code> if the string does not contain any parseable
393      * string, the day otherwise.
394      */

395     public static Day parseDay(String JavaDoc s) {
396
397         try {
398             return new Day (Day.DATE_FORMAT.parse(s));
399         }
400         catch (ParseException JavaDoc e1) {
401             try {
402                 return new Day (Day.DATE_FORMAT_SHORT.parse(s));
403             }
404             catch (ParseException JavaDoc e2) {
405               // ignore
406
}
407         }
408         return null;
409
410     }
411
412 }
413
Popular Tags