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