Debian Codegraph @

src/com/adobe/internal/xmp/impl/XMPDateTimeImpl.java

raw symbols SCIP
1 // =================================================================================================
2 // ADOBE SYSTEMS INCORPORATED
3 // Copyright 2006 Adobe Systems Incorporated
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it.
8 // =================================================================================================
9
10 package com.adobe.internal.xmp.impl;
11
12 import java.util.Calendar;
13 import java.util.Date;
14 import java.util.GregorianCalendar;
15 import java.util.Locale;
16 import java.util.TimeZone;
17
18 import com.adobe.internal.xmp.XMPDateTime;
19 import com.adobe.internal.xmp.XMPException;
20
21
22 /**
23 * The implementation of <code>XMPDateTime</code>. Internally a <code>calendar</code> is used
24 * plus an additional nano seconds field, because <code>Calendar</code> supports only milli
25 * seconds. The <code>nanoSeconds</code> convers only the resolution beyond a milli second.
26 *
27 * @author Stefan Makswit
28 * @version $Revision$
29 * @since 16.02.2006
30 */
31 public class XMPDateTimeImpl implements XMPDateTime
32 {
33 /** */
34 private int year = 0;
35 /** */
36 private int month = 0;
37 /** */
38 private int day = 0;
39 /** */
40 private int hour = 0;
41 /** */
42 private int minute = 0;
43 /** */
44 private int second = 0;
45 /** Use NO time zone as default */
46 private TimeZone timeZone = null;
47 /**
48 * The nano seconds take micro and nano seconds, while the milli seconds are in the calendar.
49 */
50 private int nanoSeconds;
51 /** */
52 private boolean hasDate = false;
53 /** */
54 private boolean hasTime = false;
55 /** */
56 private boolean hasTimeZone = false;
57
58
59 /**
60 * Creates an <code>XMPDateTime</code>-instance with the current time in the default time
61 * zone.
62 */
63 public XMPDateTimeImpl()
64 {
65 // EMPTY
66 }
67
68
69 /**
70 * Creates an <code>XMPDateTime</code>-instance from a calendar.
71 *
72 * @param calendar a <code>Calendar</code>
73 */
74 public XMPDateTimeImpl(Calendar calendar)
75 {
76 // extract the date and timezone from the calendar provided
77 Date date = calendar.getTime();
78 TimeZone zone = calendar.getTimeZone();
79
80 // put that date into a calendar the pretty much represents ISO8601
81 // I use US because it is close to the "locale" for the ISO8601 spec
82 GregorianCalendar intCalendar =
83 (GregorianCalendar) Calendar.getInstance(Locale.US);
84 intCalendar.setGregorianChange(new Date(Long.MIN_VALUE));
85 intCalendar.setTimeZone(zone);
86 intCalendar.setTime(date);
87
88 this.year = intCalendar.get(Calendar.YEAR);
89 this.month = intCalendar.get(Calendar.MONTH) + 1; // cal is from 0..12
90 this.day = intCalendar.get(Calendar.DAY_OF_MONTH);
91 this.hour = intCalendar.get(Calendar.HOUR_OF_DAY);
92 this.minute = intCalendar.get(Calendar.MINUTE);
93 this.second = intCalendar.get(Calendar.SECOND);
94 this.nanoSeconds = intCalendar.get(Calendar.MILLISECOND) * 1000000;
95 this.timeZone = intCalendar.getTimeZone();
96
97 // object contains all date components
98 hasDate = hasTime = hasTimeZone = true;
99 }
100
101
102 /**
103 * Creates an <code>XMPDateTime</code>-instance from
104 * a <code>Date</code> and a <code>TimeZone</code>.
105 *
106 * @param date a date describing an absolute point in time
107 * @param timeZone a TimeZone how to interpret the date
108 */
109 public XMPDateTimeImpl(Date date, TimeZone timeZone)
110 {
111 GregorianCalendar calendar = new GregorianCalendar(timeZone);
112 calendar.setTime(date);
113
114 this.year = calendar.get(Calendar.YEAR);
115 this.month = calendar.get(Calendar.MONTH) + 1; // cal is from 0..12
116 this.day = calendar.get(Calendar.DAY_OF_MONTH);
117 this.hour = calendar.get(Calendar.HOUR_OF_DAY);
118 this.minute = calendar.get(Calendar.MINUTE);
119 this.second = calendar.get(Calendar.SECOND);
120 this.nanoSeconds = calendar.get(Calendar.MILLISECOND) * 1000000;
121 this.timeZone = timeZone;
122
123 // object contains all date components
124 hasDate = hasTime = hasTimeZone = true;
125 }
126
127
128 /**
129 * Creates an <code>XMPDateTime</code>-instance from an ISO 8601 string.
130 *
131 * @param strValue an ISO 8601 string
132 * @throws XMPException If the string is a non-conform ISO 8601 string, an exception is thrown
133 */
134 public XMPDateTimeImpl(String strValue) throws XMPException
135 {
136 ISO8601Converter.parse(strValue, this);
137 }
138
139
140 /**
141 * @see XMPDateTime#getYear()
142 */
143 public int getYear()
144 {
145 return year;
146 }
147
148
149 /**
150 * @see XMPDateTime#setYear(int)
151 */
152 public void setYear(int year)
153 {
154 this.year = Math.min(Math.abs(year), 9999);
155 this.hasDate = true;
156 }
157
158
159 /**
160 * @see XMPDateTime#getMonth()
161 */
162 public int getMonth()
163 {
164 return month;
165 }
166
167
168 /**
169 * @see XMPDateTime#setMonth(int)
170 */
171 public void setMonth(int month)
172 {
173 if (month < 1)
174 {
175 this.month = 1;
176 }
177 else if (month > 12)
178 {
179 this.month = 12;
180 }
181 else
182 {
183 this.month = month;
184 }
185
186 this.hasDate = true;
187 }
188
189
190 /**
191 * @see XMPDateTime#getDay()
192 */
193 public int getDay()
194 {
195 return day;
196 }
197
198
199 /**
200 * @see XMPDateTime#setDay(int)
201 */
202 public void setDay(int day)
203 {
204 if (day < 1)
205 {
206 this.day = 1;
207 }
208 else if (day > 31)
209 {
210 this.day = 31;
211 }
212 else
213 {
214 this.day = day;
215 }
216
217 this.hasDate = true;
218 }
219
220
221 /**
222 * @see XMPDateTime#getHour()
223 */
224 public int getHour()
225 {
226 return hour;
227 }
228
229
230 /**
231 * @see XMPDateTime#setHour(int)
232 */
233 public void setHour(int hour)
234 {
235 this.hour = Math.min(Math.abs(hour), 23);
236 this.hasTime = true;
237 }
238
239
240 /**
241 * @see XMPDateTime#getMinute()
242 */
243 public int getMinute()
244 {
245 return minute;
246 }
247
248
249 /**
250 * @see XMPDateTime#setMinute(int)
251 */
252 public void setMinute(int minute)
253 {
254 this.minute = Math.min(Math.abs(minute), 59);
255 this.hasTime = true;
256 }
257
258
259 /**
260 * @see XMPDateTime#getSecond()
261 */
262 public int getSecond()
263 {
264 return second;
265 }
266
267
268 /**
269 * @see XMPDateTime#setSecond(int)
270 */
271 public void setSecond(int second)
272 {
273 this.second = Math.min(Math.abs(second), 59);
274 this.hasTime = true;
275 }
276
277
278 /**
279 * @see XMPDateTime#getNanoSecond()
280 */
281 public int getNanoSecond()
282 {
283 return nanoSeconds;
284 }
285
286
287 /**
288 * @see XMPDateTime#setNanoSecond(int)
289 */
290 public void setNanoSecond(int nanoSecond)
291 {
292 this.nanoSeconds = nanoSecond;
293 this.hasTime = true;
294 }
295
296
297 /**
298 * @see Comparable#compareTo(Object)
299 */
300 public int compareTo(Object dt)
301 {
302 long d = getCalendar().getTimeInMillis()
303 - ((XMPDateTime) dt).getCalendar().getTimeInMillis();
304 if (d != 0)
305 {
306 return (int) Math.signum(d);
307 }
308 else
309 {
310 // if millis are equal, compare nanoseconds
311 d = nanoSeconds - ((XMPDateTime) dt).getNanoSecond();
312 return (int) Math.signum(d);
313 }
314 }
315
316
317 /**
318 * @see XMPDateTime#getTimeZone()
319 */
320 public TimeZone getTimeZone()
321 {
322 return timeZone;
323 }
324
325
326 /**
327 * @see XMPDateTime#setTimeZone(TimeZone)
328 */
329 public void setTimeZone(TimeZone timeZone)
330 {
331 this.timeZone = timeZone;
332 this.hasTime = true;
333 this.hasTimeZone = true;
334 }
335
336
337 /**
338 * @see XMPDateTime#hasDate()
339 */
340 public boolean hasDate()
341 {
342 return this.hasDate;
343 }
344
345
346 /**
347 * @see XMPDateTime#hasTime()
348 */
349 public boolean hasTime()
350 {
351 return this.hasTime;
352 }
353
354
355 /**
356 * @see XMPDateTime#hasTimeZone()
357 */
358 public boolean hasTimeZone()
359 {
360 return this.hasTimeZone;
361 }
362
363
364 /**
365 * @see XMPDateTime#getCalendar()
366 */
367 public Calendar getCalendar()
368 {
369 GregorianCalendar calendar = (GregorianCalendar) Calendar.getInstance(Locale.US);
370 calendar.setGregorianChange(new Date(Long.MIN_VALUE));
371 if (hasTimeZone)
372 {
373 calendar.setTimeZone(timeZone);
374 }
375 calendar.set(Calendar.YEAR, year);
376 calendar.set(Calendar.MONTH, month - 1);
377 calendar.set(Calendar.DAY_OF_MONTH, day);
378 calendar.set(Calendar.HOUR_OF_DAY, hour);
379 calendar.set(Calendar.MINUTE, minute);
380 calendar.set(Calendar.SECOND, second);
381 calendar.set(Calendar.MILLISECOND, nanoSeconds / 1000000);
382
383 return calendar;
384 }
385
386
387 /**
388 * @see XMPDateTime#getISO8601String()
389 */
390 public String getISO8601String()
391 {
392 return ISO8601Converter.render(this);
393 }
394
395
396 /**
397 * @return Returns the ISO string representation.
398 */
399 public String toString()
400 {
401 return getISO8601String();
402 }
403 }