1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.model.temporal;
17
18 import java.text.DateFormat;
19 import java.util.Calendar;
20 import java.util.GregorianCalendar;
21
22 import net.sf.oness.common.all.BaseObject;
23
24 import org.apache.commons.lang.builder.EqualsBuilder;
25 import org.apache.commons.lang.builder.HashCodeBuilder;
26 import org.apache.commons.lang.time.DateUtils;
27
28 /***
29 * <p>
30 * Wrapper around a <code>Calendar</code> object truncated to the field
31 * specified (by default SECOND) using DateUtils.truncate(Calendar, int).
32 * </p>
33 * <p>
34 * The MIN_VALUE and MAX_VALUE fields represent negative and positive infinity
35 * respectively, the implementation doesn't use Calendar.getMinimum or
36 * Calendar.getMaximum because the underlying database may not accept the range
37 * of values available in Calendar
38 * </p>
39 *
40 * @see java.util.Calendar
41 * @see org.apache.commons.lang.time.DateUtils#truncate(Calendar,int)
42 *
43 * @author Carlos Sanchez
44 * @version $Revision: 1.6 $
45 */
46 public class Date extends BaseObject implements Comparable {
47
48 private static final DateFormat formatter = DateFormat
49 .getDateTimeInstance();
50
51 /***
52 * The min value (actually 1000-JAN-01 00:00:00)
53 */
54 public static final Date MIN_VALUE = new Date(1000, Calendar.JANUARY, 1, 0,
55 0, 0);
56
57 /***
58 * The max value (actually 9999-DEC-31 23:59:59)
59 */
60 public static final Date MAX_VALUE = new Date(9999, Calendar.DECEMBER, 31,
61 23, 59, 59);
62
63 private final static int DEFAULT_TRUNCATE_TO_FIELD = Calendar.SECOND;
64
65 private int truncateToField = DEFAULT_TRUNCATE_TO_FIELD;
66
67 /***
68 * Wrapped calendar
69 */
70 protected Calendar calendar;
71
72 /***
73 * Create Date not initialized.
74 */
75 public Date() {
76 }
77
78 /***
79 * Create a Date from year, month, day, hour, minute, second
80 *
81 * @param year
82 * @param month
83 * @param day
84 * @param hour
85 * @param minute
86 * @param second
87 */
88 public Date(int year, int month, int day, int hour, int minute, int second) {
89 calendar = new GregorianCalendar(year, month, day, hour, minute, second);
90 this.truncateToField = Calendar.SECOND;
91 }
92
93 /***
94 * Create a Date from year, month, day
95 *
96 * @param year
97 * @param month
98 * @param day
99 */
100 public Date(int year, int month, int day) {
101 calendar = new GregorianCalendar(year, month, day);
102 this.truncateToField = Calendar.DAY_OF_MONTH;
103 }
104
105 /***
106 * Create a date from a Calendar truncated to the field specified leaving it
107 * as the most significant. <br/>Future changes to the calendar will not
108 * affect the newly created object
109 *
110 * @param calendar
111 * @param truncateToField
112 */
113 public Date(Calendar calendar, int truncateToField) {
114 this.calendar = DateUtils.truncate(calendar, truncateToField);
115 this.truncateToField = truncateToField;
116
117 }
118
119 /***
120 * Create a date from a Calendar truncated to the default field
121 * {@link DEFAULT_TRUNCATE_TO_FIELD}leaving it as the most significant.
122 * <br/>Future changes to the calendar will not affect the newly created
123 * object
124 *
125 * @param calendar
126 */
127 public Date(Calendar calendar) {
128 this.calendar = DateUtils.truncate(calendar, DEFAULT_TRUNCATE_TO_FIELD);
129 }
130
131 /***
132 * Create a Date for the current time and truncated to the default field
133 * {@link DEFAULT_TRUNCATE_TO_FIELD}leaving it as the most significant.
134 */
135 public static Date now() {
136 return new Date(DateUtils.truncate(Calendar.getInstance(),
137 DEFAULT_TRUNCATE_TO_FIELD));
138 }
139
140 /***
141 * Get the <code>Calendar<code> wrapped by this object.
142 * Changes to the Calendar will affect this date.
143 * @return the calendar
144 */
145 public Calendar getCalendar() {
146 return calendar;
147 }
148
149 /***
150 * Set the <code>Calendar<code> wrapped by this object.
151 * Changes to the Calendar will affect this date.
152 * @param calendar
153 */
154 public void setCalendar(Calendar calendar) {
155 this.calendar = calendar;
156 }
157
158 /***
159 * @see Calendar#after(java.lang.Object)
160 * @param when
161 * @return
162 */
163 public boolean after(Date when) {
164 return calendar.after(when.calendar);
165 }
166
167 /***
168 * @see Calendar#before(java.lang.Object)
169 * @param when
170 * @return
171 */
172 public boolean before(Date when) {
173 return calendar.before(when.calendar);
174 }
175
176 /***
177 * @see java.util.Date#compareTo(java.lang.Object)
178 * @param arg
179 * @return
180 */
181 public int compareTo(Object arg) {
182 Date other = (Date) arg;
183 return getTime().compareTo(other.getTime());
184 }
185
186 /***
187 * Get this Time as a <code>java.util.Date<code> object.
188 * @see Calendar#getTime()
189 * @return
190 */
191 public java.util.Date getTime() {
192 return calendar.getTime();
193 }
194
195 public void addDays(int arg) {
196 calendar.add(Calendar.DAY_OF_MONTH, arg);
197 }
198
199 public void addYears(int arg) {
200 calendar.add(Calendar.YEAR, arg);
201 }
202
203 public int getYear() {
204 return calendar.get(Calendar.YEAR);
205 }
206
207 public int getMonth() {
208 return calendar.get(Calendar.MONTH);
209 }
210
211 public int getDayOfMonth() {
212 return calendar.get(Calendar.DAY_OF_MONTH);
213 }
214
215 public String toString() {
216 if (this.equals(MIN_VALUE))
217 return "-infinity";
218 if (this.equals(MAX_VALUE))
219 return "+infinity";
220 return formatter.format(getTime());
221 }
222
223 /***
224 * @see java.lang.Object#equals(java.lang.Object)
225 */
226 public boolean equals(Object o) {
227 if (!(o instanceof Date)) {
228 return false;
229 }
230 Date rhs = (Date) o;
231 return new EqualsBuilder().append(calendar, rhs.calendar).isEquals();
232 }
233
234 /***
235 * @see java.lang.Object#hashCode()
236 */
237 public int hashCode() {
238 return new HashCodeBuilder(19, 41).append(calendar).toHashCode();
239 }
240
241 /***
242 * @see java.lang.Object#clone()
243 */
244 public Object clone() {
245 return new Date((Calendar) calendar.clone());
246 }
247
248 }