001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.datatypes;
051:
052: import java.io.Serializable;
053: import java.util.Date;
054: import java.util.Calendar;
055: import java.util.GregorianCalendar; //import java.sql.Date;
056: import java.sql.Time;
057: import java.sql.Timestamp;
058: import java.text.ParseException;
059:
060: /**
061: * This class is used to hold DateTime data. Instances of this class are immutable.
062: */
063: public class DateTime implements Cloneable, Comparable, Serializable,
064: IDateBase {
065:
066: // *** PRIVATE FIELDS ***
067: // NOTE: keep the clone() method in sync for all the fields added/removed
068: private DateBase m_datetime = null;
069:
070: // *** CONSTRUCTORS ***
071: /** Creates an instance with the current datetime.
072: */
073: public DateTime() {
074: m_datetime = new DateBase();
075: }
076:
077: /** Creates an instance initialized to the input value.
078: * @param timeInMillis the initial value.
079: */
080: public DateTime(long timeInMillis) {
081: m_datetime = new DateBase(timeInMillis);
082: }
083:
084: /** Creates an instance initialized to the input value.
085: * @param utilDate the initial value.
086: */
087: public DateTime(java.util.Date utilDate) {
088: m_datetime = new DateBase(utilDate);
089: }
090:
091: /** Creates an instance initialized to the input value.
092: * @param calendar the initial value.
093: */
094: public DateTime(Calendar calendar) {
095: m_datetime = new DateBase(calendar);
096: }
097:
098: /** Creates an instance initialized to the input value.
099: * @param year the year.
100: * @param month the month.
101: * @param day the day.
102: */
103: public DateTime(int year, int month, int day) {
104: m_datetime = new DateBase(year, month, day);
105: }
106:
107: /** Creates an instance initialized to the input value.
108: * @param year the year.
109: * @param month the month.
110: * @param day the day.
111: * @param hourOfDay the hour of the day (i.e. a 24 hour clock).
112: * @param minute the minute.
113: * @param second the second.
114: * @param milli the milli.
115: */
116: public DateTime(int year, int month, int day, int hourOfDay,
117: int minute, int second, int milli) {
118: m_datetime = new DateBase(year, month, day, hourOfDay, minute,
119: second, milli);
120: }
121:
122: // *** PUBLIC METHODS ***
123: // - Some standard Methods
124: /** Returns a clone of the object.
125: * @throws CloneNotSupportedException if cloning is not supported. This should never happen.
126: * @return a clone of the object.
127: */
128: public Object clone() throws CloneNotSupportedException {
129: Object obj = super .clone();
130: if (m_datetime != null)
131: ((DateTime) obj).m_datetime = (DateBase) m_datetime.clone();
132: return obj;
133: }
134:
135: /** Compares this object with another DateTime object.
136: * @param obj the other DateTime object.
137: * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
138: */
139: public int compareTo(Object obj) {
140: DateTime target = (DateTime) obj;
141: return m_datetime.compareTo(target.m_datetime);
142: }
143:
144: /** Compares this object with another DateTime object.
145: * Returns a true if both the objects have the same value.
146: * @param obj the other DateTime object.
147: * @return a true if both the objects have the same value.
148: */
149: public boolean equals(Object obj) {
150: if (obj instanceof DateTime) {
151: DateTime target = (DateTime) obj;
152: return m_datetime.equals(target.m_datetime);
153: } else {
154: return false;
155: }
156: }
157:
158: /** Returns the hashCode of the value.
159: * @return the hashCode of the value.
160: */
161: public int hashCode() {
162: return m_datetime.hashCode();
163: }
164:
165: /** Returns the diagnostic information.
166: * @return the diagnostic information.
167: */
168: public String toString() {
169: return Formatter.format(this );
170: }
171:
172: // - Return standard Java Date Objects
173:
174: /** Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this object.
175: * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this object.
176: */
177: public long getTime() {
178: return m_datetime.getTime();
179: }
180:
181: /** Returns the value as a java.util.Date object.
182: * @return the value as a java.util.Date object.
183: */
184: public java.util.Date getUtilDate() {
185: //log.debug("Get util Date From DateTime : " + this.toString() + " = " + m_datetime.getUtilDate() );
186: return m_datetime.getUtilDate();
187: }
188:
189: /** Returns the Calendar.
190: * @return the Calendar.
191: */
192: public Calendar getCalendar() {
193: return m_datetime.getCalendar();
194: }
195:
196: /** Returns the value as a java.sql.Date object.
197: * @return the value as a java.sql.Date object.
198: */
199: public java.sql.Date getSQLDate() {
200: return m_datetime.getSQLDate();
201: }
202:
203: /** Returns the value as a java.sql.Time object.
204: * @return the value as a java.sql.Time object.
205: */
206: public Time getSQLTime() {
207: return m_datetime.getSQLTime();
208: }
209:
210: /** Returns the value as a java.sql.Timestamp object.
211: * @return the value as a java.sql.Timestamp object.
212: */
213: public Timestamp getTimestamp() {
214: return m_datetime.getTimestamp();
215: }
216:
217: // - Additional Getters
218:
219: /** Returns the year.
220: * @return the year.
221: */
222: public int getYear() {
223: return m_datetime.getYear();
224: }
225:
226: /** Returns the month.
227: * @return the month.
228: */
229: public int getMonth() {
230: return m_datetime.getMonth();
231: }
232:
233: /** Returns the day.
234: * @return the day.
235: */
236: public int getDay() {
237: return m_datetime.getDay();
238: }
239:
240: /** Returns the day of the week.
241: * @return the day of the week.
242: */
243: public int getDayOfWeek() {
244: return m_datetime.getDayOfWeek();
245: }
246:
247: /** Returns the day of the week in the month.
248: * @return the day of the week in the month.
249: */
250: public int getDayOfWeekInMonth() {
251: return m_datetime.getDayOfWeekInMonth();
252: }
253:
254: /** Returns the day of the year.
255: * @return the day of the year.
256: */
257: public int getDayOfYear() {
258: return m_datetime.getDayOfYear();
259: }
260:
261: /** Returns the week of the month.
262: * @return the week of the month.
263: */
264: public int getWeekOfMonth() {
265: return m_datetime.getWeekOfMonth();
266: }
267:
268: /** Returns the week of the year.
269: * @return the week of the year.
270: */
271: public int getWeekOfYear() {
272: return m_datetime.getWeekOfYear();
273: }
274:
275: /** Returns the hour.
276: * @return the hour.
277: */
278: public int getHour() {
279: return m_datetime.getHour();
280: }
281:
282: /** Returns the hour (a 24 hour clock).
283: * @return the hour.
284: */
285: public int getHourOfDay() {
286: return m_datetime.getHourOfDay();
287: }
288:
289: /** Returns the minute.
290: * @return the minute.
291: */
292: public int getMinute() {
293: return m_datetime.getMinute();
294: }
295:
296: /** Returns the second.
297: * @return the second.
298: */
299: public int getSecond() {
300: return m_datetime.getSecond();
301: }
302:
303: /** Returns the milliseconds.
304: * @return the milliseconds.
305: */
306: public int getMilli() {
307: return m_datetime.getMilli();
308: }
309:
310: /** Returns a true if the datetime is before 12:00 pm.
311: * @return a true if the datetime is before 12:00 pm.
312: */
313: public boolean isAM() {
314: return m_datetime.isAM();
315: }
316:
317: /** Returns a true if the datetime is after 12:00 pm.
318: * @return a true if the datetime is after 12:00 pm.
319: */
320: public boolean isPM() {
321: return m_datetime.isPM();
322: }
323:
324: /** Returns the first day of the week.
325: * @return the first day of the week.
326: */
327: public int getFirstDayOfWeek() {
328: return m_datetime.getFirstDayOfWeek();
329: }
330:
331: /** Returns a true if this date is after the input date.
332: * @param when the input date.
333: * @return a true if this date is after the input date.
334: */
335: public boolean isAfter(DateTime when) {
336: return m_datetime.isAfter(when.m_datetime);
337: }
338:
339: /** Returns a true if this date is before the input date.
340: * @param when the input date.
341: * @return a true if this date is before the input date.
342: */
343: public boolean isBefore(DateTime when) {
344: return m_datetime.isBefore(when.m_datetime);
345: }
346:
347: /** Returns the corresponding julian value.
348: * @return the corresponding julian value.
349: */
350: public int getJulian() {
351: return m_datetime.getJulian();
352: }
353:
354: // - Date Manipulation methods
355:
356: /** Create a new DateTime object by adding the input years to the value of the input datetime.
357: * If the input years is a negative number, then the years will be subtracted.
358: * @param datetime the input datetime.
359: * @param years the years to add.
360: * @return a DateTime object with the value of the input date, incremented by the input years.
361: */
362: public static DateTime addYear(DateTime datetime, int years) {
363: return new DateTime(DateBase
364: .addYear(datetime.m_datetime, years).getTime());
365: }
366:
367: /** Create a new DateTime object by adding the input months to the value of the input datetime.
368: * If the input months is a negative number, then the months will be subtracted.
369: * @param datetime the input datetime.
370: * @param months the months to add.
371: * @return a DateTime object with the value of the input date, incremented by the input months.
372: */
373: public static DateTime addMonth(DateTime datetime, int months) {
374: return new DateTime(DateBase.addMonth(datetime.m_datetime,
375: months).getTime());
376: }
377:
378: /** Create a new DateTime object by adding the input days to the value of the input datetime.
379: * If the input days is a negative number, then the days will be subtracted.
380: * @param datetime the input datetime.
381: * @param days the days to add.
382: * @return a DateTime object with the value of the input date, incremented by the input days.
383: */
384: public static DateTime addDay(DateTime datetime, int days) {
385: return new DateTime(DateBase.addDay(datetime.m_datetime, days)
386: .getTime());
387: }
388:
389: /** Create a new DateTime object by adding the input hours to the value of the input datetime.
390: * If the input hours is a negative number, then the hours will be subtracted.
391: * @param datetime the input datetime.
392: * @param hours the hours to add.
393: * @return a DateTime object with the value of the input date, incremented by the input hours.
394: */
395: public static DateTime addHour(DateTime datetime, int hours) {
396: return new DateTime(DateBase
397: .addHour(datetime.m_datetime, hours).getTime());
398: }
399:
400: /** Create a new DateTime object by adding the input minutes to the value of the input datetime.
401: * If the input minutes is a negative number, then the minutes will be subtracted.
402: * @param datetime the input datetime.
403: * @param minutes the minutes to add.
404: * @return a DateTime object with the value of the input date, incremented by the input minutes.
405: */
406: public static DateTime addMinute(DateTime datetime, int minutes) {
407: return new DateTime(DateBase.addMinute(datetime.m_datetime,
408: minutes).getTime());
409: }
410:
411: /** Create a new DateTime object by adding the input seconds to the value of the input datetime.
412: * If the input seconds is a negative number, then the seconds will be subtracted.
413: * @param datetime the input datetime.
414: * @param seconds the seconds to add.
415: * @return a DateTime object with the value of the input date, incremented by the input seconds.
416: */
417: public static DateTime addSecond(DateTime datetime, int seconds) {
418: return new DateTime(DateBase.addSecond(datetime.m_datetime,
419: seconds).getTime());
420: }
421:
422: /** Create a new DateTime object by adding the input milliseconds to the value of the input datetime.
423: * If the input milliseconds is a negative number, then the milliseconds will be subtracted.
424: * @param datetime the input datetime.
425: * @param millis the milliseconds to add.
426: * @return a DateTime object with the value of the input date, incremented by the input milliseconds.
427: */
428: public static DateTime addMilli(DateTime datetime, int millis) {
429: return new DateTime(DateBase.addMilli(datetime.m_datetime,
430: millis).getTime());
431: }
432:
433: /** Returns the number of days between two dates.
434: * @param date1 the first date.
435: * @param date2 the second date.
436: * @return the number of days between two dates.
437: */
438: public static float daysBetween(DateTime date1, DateTime date2) {
439: return DateBase.daysBetween(date1.m_datetime, date2.m_datetime);
440: }
441:
442: /** Returns a new DateTime object, having the date of the input object, but the time portion set to zero.
443: * @param datetime the input datetime.
444: * @return a new DateTime object, having the date of the input object, but the time portion set to zero.
445: */
446: public static DateTime clearTime(DateTime datetime) {
447: return new DateTime(DateBase.clearTime(datetime.m_datetime)
448: .getTime());
449: }
450:
451: /** This parses the input String, returning a DateTime object with the corresponding value.
452: * The input String can have values of the type - N, N + 1, N - 6..., where N = current datetime.
453: * It can also have values of the type - T, T - 1, T + 2..., where T = todays date.
454: * Additionally the input String can be a formatted String based on the default layout of the DateTimeFieldMetaData.
455: * @param dateString the String to be parsed.
456: * @return a DateTime object which has the value based on the input.
457: * @throws ParseException if any error occurs in parsing.
458: */
459: public static DateTime parse(String dateString)
460: throws ParseException {
461: return parse(dateString, null);
462: }
463:
464: /** This parses the input String, returning a DateTime object with the corresponding value.
465: * The input String can have values of the type - N, N + 1, N - 6..., where N = current datetime.
466: * It can also have values of the type - T, T - 1, T + 2..., where T = todays date.
467: * Additionally the input String can be a formatted String based on the input layout or the default layout of the DateTimeFieldMetaData.
468: * @param dateString the String to be parsed.
469: * @param layout the format used by the input String.
470: * @return a DateTime object which has the value based on the input.
471: * @throws ParseException if any error occurs in parsing.
472: */
473: public static DateTime parse(String dateString, String layout)
474: throws ParseException {
475: DateTime dateTime = null;
476: if (dateString != null && dateString.length() > 0)
477: dateTime = new DateTime(DateBase.parse(dateString, layout,
478: true).getTime());
479: return dateTime;
480: }
481:
482: /** Returns a DateOnly object initialized to the input date.
483: * @param datetime the input date.
484: * @return a DateOnly object initialized to the input date.
485: */
486: public static DateOnly toDateOnly(DateTime datetime) {
487: return new DateOnly(datetime.getTime());
488: }
489:
490: }
|