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 Gregorian calendar with date and time, but without milliseconds.
32 *
33 * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
34 */
35 public final class GregorianDateTime extends GregorianCalendar {
36
37 private static final long serialVersionUID = 1L;
38
39 /***
40 * Default constructor.
41 */
42 public GregorianDateTime() {
43 this(TimeZone.getDefault());
44 }
45
46 /***
47 * Constructor.
48 *
49 * @param zone the given time zone.
50 */
51 public GregorianDateTime(final TimeZone zone) {
52 super(zone);
53 computeTime();
54 }
55
56 /***
57 * Creates a new Gregorian date from a given <code>java.util.Date</code>.
58 *
59 * @param date the date for the new calendar
60 */
61 public GregorianDateTime(final Date date) {
62 this(date, TimeZone.getDefault());
63 }
64
65 /***
66 * Creates a new Gregorian date from a given <code>java.util.Date</code>.
67 *
68 * @param date the date for the new calendar
69 * @param zone the given time zone.
70 */
71 public GregorianDateTime(final Date date, final TimeZone zone) {
72 super(zone);
73 if (date != null) {
74 setTime(date);
75 }
76 computeTime();
77 }
78
79 /***
80 * Creates a new Gregorian date from a given <code>java.util.Calendar</code>.
81 *
82 * @param calendar the original calendar for the new calendar
83 */
84 public GregorianDateTime(final Calendar calendar) {
85 if (calendar != null) {
86 setTimeZone(calendar.getTimeZone());
87 setTime(calendar.getTime());
88 set(HOUR_OF_DAY, calendar.get(HOUR_OF_DAY));
89 set(MINUTE, calendar.get(MINUTE));
90 }
91 computeTime();
92 }
93
94 /***
95 * Creates a new Gregorian date from given year, month and date.
96 *
97 * <p>The first month of the year is <code>JANUARY</code> which is 0; the
98 * last month is <code>DEDCEMBER</code> which is 11.</p>
99 *
100 * @param year the year of the new calendar
101 * @param month the month of the new calendar
102 * @param day the day of the new calendar
103 */
104 public GregorianDateTime(final int year, final int month, final int day) {
105 super(year, month, day);
106 }
107
108 /***
109 * Creates a new Gregorian date from given year, month and date.
110 *
111 * <p>The first month of the year is <code>JANUARY</code> which is 0; the
112 * last month is <code>DEDCEMBER</code> which is 11.</p>
113 *
114 * @param year the year of the new calendar
115 * @param month the month of the new calendar
116 * @param day the day of the new calendar
117 * @param hour the hour of the new calendar
118 * @param minute the minute of the new calendar
119 * @param second the second of the new calendar
120 */
121 public GregorianDateTime(final int year, final int month, final int day, final int hour, final int minute, final int second) {
122 this(year, month, day, hour, minute, second, TimeZone.getDefault());
123 }
124
125 /***
126 * Creates a new Gregorian date from given year, month and date.
127 *
128 * <p>The first month of the year is <code>JANUARY</code> which is 0; the
129 * last month is <code>DEDCEMBER</code> which is 11.</p>
130 *
131 * @param year the year of the new calendar
132 * @param month the month of the new calendar
133 * @param day the day of the new calendar
134 * @param hour the hour of the new calendar
135 * @param minute the minute of the new calendar
136 * @param second the second of the new calendar
137 * @param zone the given time zone.
138 */
139 public GregorianDateTime(final int year, final int month, final int day, final int hour, final int minute, final int second, final TimeZone zone) {
140 super(zone);
141 set(Calendar.YEAR, year);
142 set(Calendar.MONTH, month);
143 set(Calendar.DATE, day);
144 set(Calendar.HOUR_OF_DAY, hour);
145 set(Calendar.MINUTE, minute);
146 set(Calendar.SECOND, second);
147 computeTime();
148 }
149
150 /***
151 * Overwrite prevents setting time fields.
152 *
153 * @param field the given calendar field.
154 * @param value the value to be set for the given calendar field.
155 *
156 * @see java.util.Calendar#set(int, int)
157 */
158 public void set(final int field, final int value) {
159
160 if (field == Calendar.MILLISECOND) {
161 return;
162 }
163 super.set(field, value);
164 computeTime();
165 }
166
167 /***
168 * Overwrite prevents setting a time zone to keep the time unchanged and unavailable.
169 *
170 * @param zone the new time zone (ignored)
171 *
172 * @see java.util.Calendar#setTimeZone(java.util.TimeZone)
173 */
174 public void setTimeZone(final TimeZone zone) {
175 super.setTimeZone(zone);
176 computeTime();
177 }
178
179 /***
180 * Overwrites GregorianCalendar.computeTime(), to disable all time fields.
181 *
182 * @see java.util.Calendar#computeTime()
183 */
184 protected void computeTime() {
185 clearFields();
186 super.computeTime();
187 }
188
189 /***
190 * Clears the millis.
191 */
192 private void clearFields() {
193 clear(Calendar.MILLISECOND);
194 }
195
196 /***
197 * Returns the month of the calendar.
198 *
199 * @return the month
200 */
201 public int getMonth() {
202 return get(Calendar.MONTH);
203 }
204
205 /***
206 * Returns the day of the calendar.
207 *
208 * @return the day
209 */
210 public int getDay() {
211 return get(Calendar.DATE);
212 }
213
214 /***
215 * Returns the year of the calendar.
216 *
217 * @return the year
218 */
219 public int getYear() {
220 return get(Calendar.YEAR);
221 }
222
223 /***
224 * Returns the hour of the calendar.
225 *
226 * @return the hour
227 */
228 public int getHour() {
229 return get(Calendar.HOUR_OF_DAY);
230 }
231
232 /***
233 * Returns the minute of the calendar.
234 *
235 * @return the minute
236 */
237 public int getMinute() {
238 return get(Calendar.MINUTE);
239 }
240
241 /***
242 * Returns the second of the calendar.
243 *
244 * @return the second
245 */
246 public int getSecond() {
247 return get(Calendar.SECOND);
248 }
249
250 /***
251 * {@inheritDoc}
252 *
253 * @see java.util.Calendar#toString()
254 */
255 public String toString() {
256 return DateUtil.getDateTimeString(this);
257 }
258 }