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