1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package org.exolab.castor.types;
39
40 import java.text.ParseException;
41 import java.util.Calendar;
42 import java.util.GregorianCalendar;
43
44
45
46
47
48
49
50
51
52
53
54 public class Date extends DateTimeBase {
55
56 private static final long serialVersionUID = -1634875709019365137L;
57
58 private static final String BAD_DATE = "Bad Date format: ";
59
60
61
62
63 public Date() {
64
65 }
66
67
68
69
70
71
72
73 public Date(short[] values) {
74 setValues(values);
75 }
76
77
78
79
80
81
82
83
84
85
86 public Date(long dateAsLong) {
87 this(new java.util.Date(dateAsLong));
88 }
89
90
91
92
93
94
95
96
97
98 public Date(java.util.Date dateRef) {
99 GregorianCalendar tempCalendar = new GregorianCalendar();
100 tempCalendar.setTime(dateRef);
101 setCentury((short) (tempCalendar.get(Calendar.YEAR) / 100));
102 setYear((short) (tempCalendar.get(Calendar.YEAR) % 100));
103
104
105
106 setMonth((short) (tempCalendar.get(Calendar.MONTH) + 1));
107 setDay((short) tempCalendar.get(Calendar.DAY_OF_MONTH));
108 }
109
110
111
112
113
114
115
116
117 public Date(String date) throws java.text.ParseException {
118 parseDateInternal(date, this);
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public void setValues(short[] values) {
137 if (values.length != 4) {
138 throw new IllegalArgumentException("Date#setValues: not the right number of values");
139 }
140 this.setCentury(values[0]);
141 this.setYear(values[1]);
142 this.setMonth(values[2]);
143 this.setDay(values[3]);
144 }
145
146
147
148
149
150
151
152
153 public short[] getValues() {
154 short[] result = new short[4];
155 result[0] = this.getCentury();
156 result[1] = this.getYear();
157 result[2] = this.getMonth();
158 result[3] = this.getDay();
159 return result;
160 }
161
162
163
164
165
166
167 public java.util.Date toDate() {
168 Calendar calendar =
169 new GregorianCalendar(getCentury() * 100 + getYear(), getMonth() - 1, getDay());
170 setDateFormatTimeZone(calendar);
171 return calendar.getTime();
172 }
173
174
175
176
177
178
179 public long toLong() {
180 return this.toDate().getTime();
181 }
182
183
184
185
186
187
188
189 public String toString() {
190 StringBuffer result = new StringBuffer();
191
192 appendDateString(result);
193 appendTimeZoneString(result);
194
195 return result.toString();
196 }
197
198
199
200
201
202
203
204
205
206 public static Object parse(String str) throws ParseException {
207 return parseDate(str);
208 }
209
210
211
212
213
214
215
216
217
218 public static Date parseDate(String str) throws ParseException {
219 Date result = new Date();
220 return parseDateInternal(str, result);
221 }
222
223 private static Date parseDateInternal(String str, Date result) throws ParseException {
224 if (str == null) {
225 throw new IllegalArgumentException("The string to be parsed must not be null.");
226 }
227
228 if (result == null) {
229 result = new Date();
230 }
231
232 char[] chars = str.toCharArray();
233
234
235 int idx = 0;
236 if (chars[idx] == '-') {
237 idx++;
238 result.setNegative();
239 }
240
241 if (!Character.isDigit(chars[idx]) || !Character.isDigit(chars[idx + 1])
242 || !Character.isDigit(chars[idx + 2]) || !Character.isDigit(chars[idx + 3])) {
243 throw new ParseException(BAD_DATE + "'" + str + "'\nThe Year must be 4 digits long", idx);
244 }
245
246 short value1;
247 short value2;
248
249 value1 = (short) ((chars[idx] - '0') * 10 + (chars[idx + 1] - '0'));
250 value2 = (short) ((chars[idx + 2] - '0') * 10 + (chars[idx + 3] - '0'));
251
252 if (value1 == 0 && value2 == 0) {
253 throw new ParseException(BAD_DATE + str + "\n'0000' is not allowed as a year.", idx);
254 }
255
256 result.setCentury(value1);
257 result.setYear(value2);
258
259 idx += 4;
260
261
262 if (chars[idx] != '-') {
263 throw new ParseException(BAD_DATE + "'" + str + "'\n '-' " + DateTimeBase.WRONGLY_PLACED,
264 idx);
265 }
266
267 idx++;
268
269 if (!Character.isDigit(chars[idx]) || !Character.isDigit(chars[idx + 1])) {
270 throw new ParseException(BAD_DATE + "'" + str + "'\nThe Month must be 2 digits long", idx);
271 }
272
273 value1 = (short) ((chars[idx] - '0') * 10 + (chars[idx + 1] - '0'));
274 result.setMonth(value1);
275
276 idx += 2;
277
278
279 if (chars[idx] != '-') {
280 throw new ParseException(BAD_DATE + "'" + str + "'\n '-' " + DateTimeBase.WRONGLY_PLACED,
281 idx);
282 }
283
284 idx++;
285
286 if (!Character.isDigit(chars[idx]) || !Character.isDigit(chars[idx + 1])) {
287 throw new ParseException(BAD_DATE + "'" + str + "'\nThe Day must be 2 digits long", idx);
288 }
289
290 value1 = (short) ((chars[idx] - '0') * 10 + (chars[idx + 1] - '0'));
291 result.setDay(value1);
292
293 idx += 2;
294
295 parseTimeZone(str, result, chars, idx, BAD_DATE);
296
297 return result;
298 }
299
300
301
302 public boolean hasHour() {
303 return false;
304 }
305
306 public short getHour() {
307 String err = "org.exolab.castor.types.Date does not have an Hour field.";
308 throw new UnsupportedOperationException(err);
309 }
310
311 public void setHour(short hour) {
312 String err = "org.exolab.castor.types.Date does not have an Hour field.";
313 throw new UnsupportedOperationException(err);
314 }
315
316 public boolean hasMinute() {
317 return false;
318 }
319
320 public short getMinute() {
321 String err = "org.exolab.castor.types.Date does not have a Minute field.";
322 throw new UnsupportedOperationException(err);
323 }
324
325 public void setMinute(short minute) {
326 String err = "org.exolab.castor.types.Date does not have a Minute field.";
327 throw new UnsupportedOperationException(err);
328 }
329
330 public boolean hasSeconds() {
331 return false;
332 }
333
334 public short getSeconds() {
335 String err = "org.exolab.castor.types.Date does not have a Seconds field.";
336 throw new UnsupportedOperationException(err);
337 }
338
339 public void setSecond(short second) {
340 String err = "org.exolab.castor.types.Date does not have a Seconds field.";
341 throw new UnsupportedOperationException(err);
342 }
343
344 public boolean hasMilli() {
345 return false;
346 }
347
348 public short getMilli() {
349 String err = "org.exolab.castor.types.Date does not have a Milliseconds field.";
350 throw new UnsupportedOperationException(err);
351 }
352
353 public void setMilliSecond(short millisecond) {
354 String err = "org.exolab.castor.types.Date does not have a Milliseconds field.";
355 throw new UnsupportedOperationException(err);
356 }
357
358 }