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 DateOnly data. Instances of this class are immutable.
062: */
063: public class DateOnly 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_date = null;
069:
070: // *** CONSTRUCTORS ***
071: /** Creates an instance with the current date.
072: */
073: public DateOnly() {
074: this (new DateBase());
075: }
076:
077: /** Creates an instance initialized to the input value.
078: * @param timeInMillis the initial value.
079: */
080: public DateOnly(long timeInMillis) {
081: this (new DateBase(timeInMillis));
082: }
083:
084: /** Creates an instance initialized to the input value.
085: * @param utilDate the initial value.
086: */
087: public DateOnly(java.util.Date utilDate) {
088: this (new DateBase(utilDate));
089: }
090:
091: /** Creates an instance initialized to the input value.
092: * @param calendar the initial value.
093: */
094: public DateOnly(Calendar calendar) {
095: this (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 DateOnly(int year, int month, int day) {
104: m_date = new DateBase(year, month, day);
105: }
106:
107: private DateOnly(DateBase aDateBase) {
108: m_date = DateBase.clearTime(aDateBase);
109: }
110:
111: // *** PUBLIC METHODS ***
112: // - Some standard Methods
113:
114: /** Returns a clone of the object.
115: * @throws CloneNotSupportedException if cloning is not supported. This should never happen.
116: * @return a clone of the object.
117: */
118: public Object clone() throws CloneNotSupportedException {
119: Object obj = super .clone();
120: if (m_date != null)
121: ((DateOnly) obj).m_date = (DateBase) m_date.clone();
122: return obj;
123: }
124:
125: /** Compares this object with another DateOnly object.
126: * @param obj the other DateOnly object.
127: * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
128: */
129: public int compareTo(Object obj) {
130: DateOnly target = (DateOnly) obj;
131: return m_date.compareTo(target.m_date);
132: }
133:
134: /** Compares this object with another DateOnly object.
135: * Returns a true if both the objects have the same value.
136: * @param obj the other DateOnly object.
137: * @return a true if both the objects have the same value.
138: */
139: public boolean equals(Object obj) {
140: if (obj instanceof DateOnly) {
141: DateOnly target = (DateOnly) obj;
142: return m_date.equals(target.m_date);
143: } else {
144: return false;
145: }
146: }
147:
148: /** Returns the hashCode of the value.
149: * @return the hashCode of the value.
150: */
151: public int hashCode() {
152: return m_date.hashCode();
153: }
154:
155: /** Returns the diagnostic information.
156: * @return the diagnostic information.
157: */
158: public String toString() {
159: return Formatter.format(this );
160: }
161:
162: // - Return standard Java Date Objects
163:
164: /** Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this object.
165: * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this object.
166: */
167: public long getTime() {
168: return m_date.getTime();
169: }
170:
171: /** Returns the value as a java.util.Date object.
172: * @return the value as a java.util.Date object.
173: */
174: public java.util.Date getUtilDate() {
175: return m_date.getUtilDate();
176: }
177:
178: /** Returns the Calendar.
179: * @return the Calendar.
180: */
181: public Calendar getCalendar() {
182: return m_date.getCalendar();
183: }
184:
185: /** Returns the value as a java.sql.Date object.
186: * @return the value as a java.sql.Date object.
187: */
188: public java.sql.Date getSQLDate() {
189: return m_date.getSQLDate();
190: }
191:
192: /** Returns the value as a java.sql.Time object.
193: * @return the value as a java.sql.Time object.
194: */
195: public Time getSQLTime() {
196: return m_date.getSQLTime();
197: }
198:
199: /** Returns the value as a java.sql.Timestamp object.
200: * @return the value as a java.sql.Timestamp object.
201: */
202: public Timestamp getTimestamp() {
203: return m_date.getTimestamp();
204: }
205:
206: // - Additional Getters
207:
208: /** Returns the year.
209: * @return the year.
210: */
211: public int getYear() {
212: return m_date.getYear();
213: }
214:
215: /** Returns the month.
216: * @return the month.
217: */
218: public int getMonth() {
219: return m_date.getMonth();
220: }
221:
222: /** Returns the day.
223: * @return the day.
224: */
225: public int getDay() {
226: return m_date.getDay();
227: }
228:
229: /** Returns the day of the week.
230: * @return the day of the week.
231: */
232: public int getDayOfWeek() {
233: return m_date.getDayOfWeek();
234: }
235:
236: /** Returns the day of the week in the month.
237: * @return the day of the week in the month.
238: */
239: public int getDayOfWeekInMonth() {
240: return m_date.getDayOfWeekInMonth();
241: }
242:
243: /** Returns the day of the year.
244: * @return the day of the year.
245: */
246: public int getDayOfYear() {
247: return m_date.getDayOfYear();
248: }
249:
250: /** Returns the week of the month.
251: * @return the week of the month.
252: */
253: public int getWeekOfMonth() {
254: return m_date.getWeekOfMonth();
255: }
256:
257: /** Returns the week of the year.
258: * @return the week of the year.
259: */
260: public int getWeekOfYear() {
261: return m_date.getWeekOfYear();
262: }
263:
264: /** Returns the first day of the week.
265: * @return the first day of the week.
266: */
267: public int getFirstDayOfWeek() {
268: return m_date.getFirstDayOfWeek();
269: }
270:
271: /** Returns a true if this date is after the input date.
272: * @param when the input date.
273: * @return a true if this date is after the input date.
274: */
275: public boolean isAfter(DateOnly when) {
276: return m_date.isAfter(when.m_date);
277: }
278:
279: /** Returns a true if this date is before the input date.
280: * @param when the input date.
281: * @return a true if this date is before the input date.
282: */
283: public boolean isBefore(DateOnly when) {
284: return m_date.isBefore(when.m_date);
285: }
286:
287: /** Returns the corresponding julian value.
288: * @return the corresponding julian value.
289: */
290: public int getJulian() {
291: return m_date.getJulian();
292: }
293:
294: // - Date Manipulation methods
295:
296: /** Create a new DateOnly object by adding the input years to the value of the input date.
297: * If the input years is a negative number, then the years will be subtracted.
298: * @param date the input date.
299: * @param years the years to add.
300: * @return a DateOnly object with the value of the input date, incremented by the input years.
301: */
302: public static DateOnly addYear(DateOnly date, int years) {
303: return new DateOnly(DateBase.addYear(date.m_date, years));
304: }
305:
306: /** Create a new DateOnly object by adding the input months to the value of the input date.
307: * If the input months is a negative number, then the months will be subtracted.
308: * @param date the input date.
309: * @param months the months to add.
310: * @return a DateOnly object with the value of the input date, incremented by the input months.
311: */
312: public static DateOnly addMonth(DateOnly date, int months) {
313: return new DateOnly(DateBase.addMonth(date.m_date, months));
314: }
315:
316: /** Create a new DateOnly object by adding the input days to the value of the input date.
317: * If the input days is a negative number, then the days will be subtracted.
318: * @param date the input date.
319: * @param days the days to add.
320: * @return a DateOnly object with the value of the input date, incremented by the input days.
321: */
322: public static DateOnly addDay(DateOnly date, int days) {
323: return new DateOnly(DateBase.addDay(date.m_date, days));
324: }
325:
326: /** Returns the number of days between two dates.
327: * @param date1 the first date.
328: * @param date2 the second date.
329: * @return the number of days between two dates.
330: */
331: public static int daysBetween(DateOnly date1, DateOnly date2) {
332: return (int) DateBase.daysBetween(date1.m_date, date2.m_date);
333: }
334:
335: /** This parses the input String, returning a DateOnly object with the corresponding value.
336: * The input String can have values of the type - N, N + 1, N - 6..., where N = current datetime.
337: * It can also have values of the type - T, T - 1, T + 2..., where T = todays date.
338: * Additionally the input String can be a formatted String based on the default layout of the DateOnlyFieldMetaData.
339: * @param dateString the String to be parsed.
340: * @return a DateOnly object which has the value based on the input.
341: * @throws ParseException if any error occurs in parsing.
342: */
343: public static DateOnly parse(String dateString)
344: throws ParseException {
345: return parse(dateString, null);
346: }
347:
348: /** This parses the input String, returning a DateOnly object with the corresponding value.
349: * The input String can have values of the type - N, N + 1, N - 6..., where N = current datetime.
350: * It can also have values of the type - T, T - 1, T + 2..., where T = todays date.
351: * Additionally the input String can be a formatted String based on the input layout or the default layout of the DateOnlyFieldMetaData.
352: * @param dateString the String to be parsed.
353: * @param layout the format used by the input String.
354: * @return a DateOnly object which has the value based on the input.
355: * @throws ParseException if any error occurs in parsing.
356: */
357: public static DateOnly parse(String dateString, String layout)
358: throws ParseException {
359: DateOnly date = null;
360: if (dateString != null && dateString.length() > 0)
361: date = new DateOnly(DateBase.parse(dateString, layout,
362: false));
363: return date;
364: }
365:
366: /** Returns a DateTime object initialized to the input date.
367: * @param date the input date.
368: * @return a DateTime object initialized to the input date.
369: */
370: public static DateTime toDateTime(DateOnly date) {
371: return new DateTime(date.getTime());
372: }
373:
374: }
|