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