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 package org.exolab.castor.types;
37
38 import java.text.ParseException;
39 import java.util.StringTokenizer;
40 import java.util.TimeZone;
41 import java.text.SimpleDateFormat;
42 import java.util.SimpleTimeZone;
43
44 import org.exolab.castor.types.TimePeriod;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class Month extends TimePeriod {
65
66 private static final long serialVersionUID = 8102039626686892932L;
67
68
69 private static final boolean DEBUG = false;
70
71
72 private static final String MONTH_FORMAT = "yyyy-MM";
73
74 public Month() {
75 super("P1M");
76
77
78 int temp = TimeZone.getDefault().getRawOffset();
79 if (temp < 0) {
80 temp = -temp;
81 try {
82 super.setZoneNegative();
83 } catch (UnsupportedOperationException e) {
84 }
85 }
86 short zhour = (short) (temp / (60 * 60 * 1000));
87 temp = temp % (60 * 60 * 1000);
88 short zmin = (short) (temp / (60 * 1000));
89 try {
90 super.setZone(zhour, zmin);
91 } catch (UnsupportedOperationException e) {
92 }
93 }
94
95
96 public void setDay(short day) throws UnsupportedOperationException {
97 String err = "In a Month : the day field must not be changed";
98 throw new UnsupportedOperationException(err);
99 }
100
101
102 public void setHour(short hour) throws UnsupportedOperationException {
103 String err = "In a Month : the hour field must not be changed";
104 throw new UnsupportedOperationException(err);
105 }
106
107 public void setMinute(short minute) throws UnsupportedOperationException {
108 String err = "In a Month : the minute field must not be changed";
109 throw new UnsupportedOperationException(err);
110 }
111
112
113 public void setSecond(short second, short millsecond) throws UnsupportedOperationException {
114 String err = "In a Month : the second fields must not be changed";
115 throw new UnsupportedOperationException(err);
116 }
117
118
119 public void setZone(short hour, short minute) throws UnsupportedOperationException {
120 String err = "In a Month : the time zone fields must not be changed";
121 throw new UnsupportedOperationException(err);
122 }
123
124
125 public void setZoneNegative() throws UnsupportedOperationException {
126 String err = "In a Month : the tinme zone fields must not be changed";
127 throw new UnsupportedOperationException(err);
128 }
129
130
131
132
133
134
135
136
137
138 public String toString() {
139
140 StringBuilder result = new StringBuilder();
141 result.append(this.getCentury());
142 if (result.length() == 1)
143 result.insert(0, 0);
144
145 if ((this.getYear() / 10) == 0)
146 result.append(0);
147 result.append(this.getYear());
148
149 result.append('-');
150 if ((this.getMonth() / 10) == 0)
151 result.append(0);
152 result.append(this.getMonth());
153
154 if (isNegative())
155 result.insert(0, '-');
156
157 return result.toString();
158 }
159
160 public static Object parse(String str) throws ParseException {
161 return parseMonth(str);
162 }
163
164
165
166
167
168
169
170
171
172 public static Month parseMonth(String str) throws ParseException {
173
174 Month result = new Month();
175
176 if (str.startsWith("-"))
177 result.setNegative();
178
179 if (DEBUG) {
180 System.out.println("In parsing method of Month");
181 System.out.println("String to parse : " + str);
182 System.out.println("Negative ? " + result.isNegative());
183 }
184
185
186 StringTokenizer token = new StringTokenizer(str, "-");
187
188 if (token.countTokens() != 2)
189 throw new ParseException(str + ": Bad XML Schema Month type format (CCYY-MM)", 0);
190
191 String temp = token.nextToken();
192 if (temp.length() != 4)
193 throw new ParseException(str + ": Bad year format", 1);
194 if (DEBUG) {
195 System.out.println("Processing century: " + temp.substring(0, 2));
196 }
197 result.setCentury(Short.parseShort(temp.substring(0, 2)));
198 if (DEBUG) {
199 System.out.println("Processing year: " + temp.substring(2, 4));
200 }
201 try {
202 result.setYear(Short.parseShort(temp.substring(2, 4)));
203 } catch (UnsupportedOperationException e) {
204 }
205 temp = token.nextToken();
206 if (temp.length() != 2)
207 throw new ParseException(str + ": Bad month format", 5);
208 if (DEBUG) {
209 System.out.println("Processing month: " + temp);
210 }
211 try {
212 result.setMonth(Short.parseShort(temp));
213 } catch (UnsupportedOperationException e) {
214 }
215 temp = null;
216 return result;
217 }
218
219 public java.util.Date toDate() throws ParseException {
220 java.util.Date date = null;
221 SimpleDateFormat df = new SimpleDateFormat(MONTH_FORMAT);
222 SimpleTimeZone timeZone = new SimpleTimeZone(0, "UTC");
223
224
225 if (!isUTC()) {
226 int offset = 0;
227 offset = ((this.getZoneMinute() + this.getZoneHour() * 60) * 60 * 1000);
228 offset = isZoneNegative() ? -offset : offset;
229 timeZone.setRawOffset(offset);
230 timeZone.setID(TimeZone.getAvailableIDs(offset)[0]);
231 }
232 df.setTimeZone(timeZone);
233 date = df.parse(this.toString());
234 return date;
235 }
236
237 }