001: /**
002: * CalendarDate.java
003: * Created on 11.02.2003, 18:02:02 Alex
004: * Package: net.sf.memoranda
005: *
006: * @author Alex V. Alishevskikh, alex@openmechanics.net
007: * Copyright (c) 2003 Memoranda Team. http://memoranda.sf.net
008: */package net.sf.memoranda.date;
009:
010: import java.text.DateFormat;
011: import java.util.Calendar;
012: import java.util.Date;
013:
014: import net.sf.memoranda.util.Local;
015: import net.sf.memoranda.util.Util;
016:
017: /**
018: *
019: */
020: /*$Id: CalendarDate.java,v 1.3 2004/01/30 12:17:41 alexeya Exp $*/
021: public class CalendarDate {
022:
023: private int _year;
024: private int _month;
025: private int _day;
026:
027: public static Calendar dateToCalendar(Date date) {
028: Calendar cal = Calendar.getInstance();
029: cal.setTime(date);
030: return cal;
031: }
032:
033: public CalendarDate() {
034: this (Calendar.getInstance());
035: }
036:
037: public CalendarDate(int day, int month, int year) {
038: _year = year;
039: _month = month;
040: Calendar cal = Calendar.getInstance();
041: cal.set(Calendar.YEAR, _year);
042: cal.set(Calendar.MONTH, _month);
043: cal.getTime();
044: int dmax = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
045: if (day <= dmax)
046: _day = day;
047: else
048: _day = dmax;
049:
050: }
051:
052: public CalendarDate(Calendar cal) {
053: _year = cal.get(Calendar.YEAR);
054: _day = cal.get(Calendar.DAY_OF_MONTH);
055: _month = cal.get(Calendar.MONTH);
056: }
057:
058: public CalendarDate(Date date) {
059: this (dateToCalendar(date));
060: }
061:
062: public CalendarDate(String date) {
063: int[] d = Util.parseDateStamp(date);
064: _day = d[0];
065: _month = d[1];
066: _year = d[2];
067:
068: }
069:
070: public static CalendarDate today() {
071: return new CalendarDate();
072: }
073:
074: public static CalendarDate yesterday() {
075: Calendar cal = Calendar.getInstance();
076: cal.roll(Calendar.DATE, false);
077: return new CalendarDate(cal);
078: }
079:
080: public static CalendarDate tomorrow() {
081: Calendar cal = Calendar.getInstance();
082: cal.roll(Calendar.DATE, true);
083: return new CalendarDate(cal);
084: }
085:
086: public static Calendar toCalendar(int day, int month, int year) {
087: Calendar cal = Calendar.getInstance();
088: cal.set(Calendar.YEAR, year);
089: cal.set(Calendar.MONTH, month);
090: cal.set(Calendar.DAY_OF_MONTH, day);
091: cal.getTime();
092: return cal;
093: }
094:
095: public static Date toDate(int day, int month, int year) {
096: Calendar cal = Calendar.getInstance();
097: cal.set(Calendar.YEAR, year);
098: cal.set(Calendar.MONTH, month);
099: cal.set(Calendar.DAY_OF_MONTH, day);
100: return cal.getTime();
101: }
102:
103: public Calendar getCalendar() {
104: return toCalendar(_day, _month, _year);
105: }
106:
107: public Date getDate() {
108: return toDate(_day, _month, _year);
109: }
110:
111: public int getDay() {
112: return _day;
113: }
114:
115: public int getMonth() {
116: return _month;
117: }
118:
119: public int getYear() {
120: return _year;
121: }
122:
123: public boolean equals(Object object) {
124: if (object.getClass().isInstance(CalendarDate.class)) {
125: CalendarDate d2 = (CalendarDate) object;
126: return ((d2.getDay() == getDay())
127: && (d2.getMonth() == getMonth()) && (d2.getYear() == getYear()));
128: } else if (object.getClass().isInstance(Calendar.class)) {
129: Calendar cal = (Calendar) object;
130: return this .equals(new CalendarDate(cal));
131: } else if (object.getClass().isInstance(Date.class)) {
132: Date d = (Date) object;
133: return this .equals(new CalendarDate(d));
134: }
135: return super .equals(object);
136: }
137:
138: public boolean equals(CalendarDate date) {
139: if (date == null)
140: return false;
141: return ((date.getDay() == getDay())
142: && (date.getMonth() == getMonth()) && (date.getYear() == getYear()));
143: }
144:
145: public boolean before(CalendarDate date) {
146: if (date == null)
147: return true;
148: return this .getCalendar().before(date.getCalendar());
149: }
150:
151: public boolean after(CalendarDate date) {
152: if (date == null)
153: return true;
154: return this .getCalendar().after(date.getCalendar());
155: }
156:
157: public boolean inPeriod(CalendarDate startDate, CalendarDate endDate) {
158: return (after(startDate) && before(endDate))
159: || equals(startDate) || equals(endDate);
160: }
161:
162: public String toString() {
163: return Util.getDateStamp(this );
164: }
165:
166: public String getFullDateString() {
167: return Local.getDateString(this , DateFormat.FULL);
168: }
169:
170: public String getMediumDateString() {
171: return Local.getDateString(this , DateFormat.MEDIUM);
172: }
173:
174: public String getLongDateString() {
175: return Local.getDateString(this , DateFormat.LONG);
176: }
177:
178: public String getShortDateString() {
179: return Local.getDateString(this, DateFormat.SHORT);
180: }
181:
182: }
|