1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.util;
24
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.GregorianCalendar;
28 import java.util.TimeZone;
29
30 /***
31 * A date-only Gregorian calendar.
32 *
33 * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
34 */
35 public final class GregorianDate extends GregorianCalendar {
36
37 /*** serial version ID for serialization. */
38 private static final long serialVersionUID = 1L;
39
40 /*** Default time zone: Greenwich meantime. */
41 private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
42
43 /***
44 * Default Constructor.
45 */
46 public GregorianDate() {
47 super(GMT);
48 computeTime();
49 }
50
51 /***
52 * Creates a new Gregorian date from a given <code>java.util.Date</code>.
53 *
54 * @param date the date for the new calendar
55 */
56 public GregorianDate(final Date date) {
57 super(GMT);
58 if (date != null) {
59 setTime(date);
60 }
61 computeTime();
62 }
63
64 /***
65 * Creates a new Gregorian date from a given <code>java.util.Calendar</code>.
66 *
67 * @param calendar the original calendar for the new calendar
68 */
69 public GregorianDate(final Calendar calendar) {
70 super(GMT);
71 if (calendar != null) {
72 setTime(calendar.getTime());
73 }
74 computeTime();
75 }
76
77 /***
78 * Creates a new Gregorian date from given year, month and date.
79 *
80 * <p>The first month of the year is <code>JANUARY</code> which is 0; the
81 * last month is <code>DEDCEMBER</code> which is 11.</p>
82 *
83 * @param year the year of the new calendar
84 * @param month the month of the new calendar
85 * @param day the day of the new calendar
86 */
87 public GregorianDate(final int year, final int month, final int day) {
88 super(GMT);
89 set(Calendar.YEAR, year);
90 set(Calendar.MONTH, month);
91 set(Calendar.DATE, day);
92 computeTime();
93 computeFields();
94 }
95
96 /***
97 * Overwrite prevents setting time fields.
98 *
99 * @param field the given calendar field.
100 * @param value the value to be set for the given calendar field.
101 *
102 * @see java.util.Calendar#set(int, int)
103 */
104 public void set(final int field, final int value) {
105 if (isTimeField(field)) {
106 return;
107 }
108 super.set(field, value);
109 }
110
111 /***
112 * Overwrite to prevent setting time fields.
113 *
114 * @param field the time field.
115 * @param amount the amount of date or time to be added to the field.
116 *
117 * @see java.util.GregorianCalendar#add(int, int)
118 */
119 public void add(final int field, final int amount) {
120 if (isTimeField(field)) {
121 return;
122 }
123 super.add(field, amount);
124 }
125
126 /***
127 * Checks if a given field number indicating a time field.
128 * @param field field index
129 * @return <code>true</code> if the field number indicating a time field, else <code>false</code>
130 */
131 private boolean isTimeField(final int field) {
132 if (field == Calendar.HOUR || field == Calendar.HOUR_OF_DAY || field == Calendar.AM_PM) {
133 return true;
134 }
135 if (field == Calendar.MINUTE || field == Calendar.SECOND) {
136 return true;
137 }
138 if (field == Calendar.MILLISECOND) {
139 return true;
140 }
141 return false;
142 }
143
144 /***
145 * Overwrite prevents setting a time zone to keep the time unchanged and unavailable.
146 *
147 * @param zone the new time zone (ignored)
148 *
149 * @see java.util.Calendar#setTimeZone(java.util.TimeZone)
150 */
151 public void setTimeZone(final TimeZone zone) {
152 }
153
154 /***
155 * Overwrites {@link Calendar#setTimeInMillis(long)}, to disable all time fields.
156 *
157 * @param millis the new time in UTC milliseconds from the epoch.
158 * @see java.util.Calendar#setTimeInMillis(long)
159 */
160 public void setTimeInMillis(final long millis) {
161 super.setTimeInMillis(millis);
162 computeTime();
163 }
164
165 /***
166 * Overwrites GregorianCalendar.computeFields(), to disable all time fields.
167 *
168 * @see java.util.Calendar#computeTime()
169 */
170 protected void computeFields() {
171 super.computeFields();
172 computeTime();
173 }
174
175 /***
176 * Overwrites GregorianCalendar.computeTime(), to disable all time fields.
177 *
178 * @see java.util.Calendar#computeTime()
179 */
180 protected void computeTime() {
181 clearFields();
182 super.computeTime();
183 }
184
185 /***
186 * Clears all time fields.
187 */
188 private void clearFields() {
189 clear(Calendar.HOUR_OF_DAY);
190 clear(Calendar.HOUR);
191 clear(Calendar.MINUTE);
192 clear(Calendar.SECOND);
193 clear(Calendar.AM_PM);
194 clear(Calendar.MILLISECOND);
195 }
196
197 /***
198 * Returns the month of the calendar.
199 *
200 * @return the month
201 */
202 public int getMonth() {
203 return get(Calendar.MONTH);
204 }
205
206 /***
207 * Returns the day of the calendar.
208 *
209 * @return the day
210 */
211 public int getDay() {
212 return get(Calendar.DATE);
213 }
214
215 /***
216 * Returns the year of the calendar.
217 *
218 * @return the year
219 */
220 public int getYear() {
221 return get(Calendar.YEAR);
222 }
223
224 /***
225 * {@inheritDoc}
226 *
227 * @see java.util.Calendar#toString()
228 */
229 public String toString() {
230 return DateUtil.getDateString(this);
231 }
232 }