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