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