KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > DCDate


1 /*
2  * DCDate.java
3  *
4  * Version: $Revision: 1.10 $
5  *
6  * Date: $Date: 2005/04/20 14:22:33 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.content;
41
42 import java.util.Calendar JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.util.GregorianCalendar JavaDoc;
45 import java.util.TimeZone JavaDoc;
46
47 import org.apache.log4j.Logger;
48
49 // FIXME: No tests
50
// FIXME: Not very robust - assumes dates will always be valid
51

52 /**
53  * Dublin Core date utility class
54  * <P>
55  * Dates in the DSpace database are held in the ISO 8601 format. They are always
56  * stored in UTC, converting to and from the current time zone.
57  * <P>
58  * <code>YYYY-MM-DDThh:mm:ss</code>
59  * <P>
60  * There are four levels of granularity, depending on how much date information
61  * is available.
62  * <P>
63  * Examples: <code>1994-05-03T15:30:24</code>,<code>1995-10-04</code>,
64  * <code>2001-10</code>,<code>1975</code>
65  *
66  * @author Robert Tansley
67  * @version $Revision: 1.10 $
68  */

69 public class DCDate
70 {
71     /** Logger */
72     private static Logger cat = Logger.getLogger(DCDate.class);
73
74     /**
75      * The month names
76      */

77     private final static String JavaDoc[] MONTHNAMES = { "January", "February",
78             "March", "April", "May", "June", "July", "August", "September",
79             "October", "November", "December" };
80
81     /** The year, or -1 if none */
82     private int year;
83
84     /** The month, or -1 if none */
85     private int month;
86
87     /** The day, or -1 if none */
88     private int day;
89
90     /** Hours, -1 if none */
91     private int hours;
92
93     /** Minutes, -1 if none */
94     private int minutes;
95
96     /** seconds, -1 if none */
97     private int seconds;
98
99     /**
100      * Calendar object for timezone conversion. Only used if the date has a time
101      * component.
102      */

103     private GregorianCalendar JavaDoc localGC;
104
105     /**
106      * Construct a clean date
107      */

108     public DCDate()
109     {
110         // Set all fields to unknown
111
year = month = day = hours = minutes = seconds = -1;
112         localGC = null;
113     }
114
115     /**
116      * Construct a date from a Dublin Core value
117      *
118      * @param fromDC
119      * the date string, in ISO 8601 (no timezone, always use UTC/GMT)
120      */

121     public DCDate(String JavaDoc fromDC)
122     {
123         // Set all fields to unknown
124
year = month = day = hours = minutes = seconds = -1;
125         localGC = null;
126
127         // An empty date is OK
128
if ((fromDC == null) || fromDC.equals(""))
129         {
130             return;
131         }
132
133         try
134         {
135             switch (fromDC.length())
136             {
137             case 20:
138
139                 // Full date and time
140
hours = Integer.parseInt(fromDC.substring(11, 13));
141                 minutes = Integer.parseInt(fromDC.substring(14, 16));
142                 seconds = Integer.parseInt(fromDC.substring(17, 19));
143
144             case 10:
145
146                 // Just full date
147
day = Integer.parseInt(fromDC.substring(8, 10));
148
149             case 7:
150
151                 // Just year and month
152
month = Integer.parseInt(fromDC.substring(5, 7));
153
154             case 4:
155
156                 // Just the year
157
year = Integer.parseInt(fromDC.substring(0, 4));
158             }
159         }
160         catch (NumberFormatException JavaDoc e)
161         {
162             // Mangled date
163
cat.warn("Mangled date: " + fromDC + " Exception: " + e);
164             year = month = day = hours = minutes = seconds = -1;
165         }
166     }
167
168     /**
169      * Construct a date object from a Java <code>Date</code> object.
170      *
171      * @param date
172      * the Java <code>Date</code> object.
173      */

174     public DCDate(Date JavaDoc date)
175     {
176         Calendar JavaDoc calendar = Calendar.getInstance();
177
178         calendar.setTime(date);
179
180         // Set all fields
181
setDateLocal(calendar.get(Calendar.YEAR),
182
183         // Uses 1 to 12 implementation below instead of Java's
184
// 0 to 11 convention
185
calendar.get(Calendar.MONTH) + 1, calendar
186                         .get(Calendar.DAY_OF_MONTH), calendar
187                         .get(Calendar.HOUR_OF_DAY), calendar
188                         .get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
189     }
190
191     /**
192      * Get a date representing the current instant in time.
193      *
194      * @return a DSpaceDate object representing the current instant.
195      */

196     public static DCDate getCurrent()
197     {
198         return (new DCDate(new Date JavaDoc()));
199     }
200
201     /**
202      * Get the date as a string to put back in the Dublin Core
203      *
204      * @return The date as a string.
205      */

206     public String JavaDoc toString()
207     {
208         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
209
210         if (year > 0)
211         {
212             sb.append(year);
213         }
214
215         if (month > 0)
216         {
217             sb.append('-').append(fleshOut(month));
218         }
219
220         if (day > 0)
221         {
222             sb.append('-').append(fleshOut(day));
223         }
224
225         if (hours > 0)
226         {
227             sb.append("T").append(fleshOut(hours)).append(':').append(
228                     fleshOut(minutes)).append(':').append(fleshOut(seconds))
229                     .append("Z");
230         }
231
232         return (sb.toString());
233     }
234
235     /**
236      * Get the date as a Java Date object
237      *
238      * @return a Date object
239      */

240     public Date JavaDoc toDate()
241     {
242         GregorianCalendar JavaDoc utcGC = new GregorianCalendar JavaDoc(TimeZone
243                 .getTimeZone("UTC"));
244
245         utcGC.set(year, month - 1, day, hours, minutes, seconds);
246
247         return utcGC.getTime();
248     }
249
250     /**
251      * Set the date. The date passed in is assumed to be in the current time
252      * zone, and is adjusting to fit the current time zone. Unknown values
253      * should be given as -1.
254      *
255      * @param yyyy
256      * the year
257      * @param mm
258      * the month
259      * @param dd
260      * the day
261      * @param hh
262      * the hours
263      * @param mn
264      * the minutes
265      * @param ss
266      * the seconds
267      */

268     public void setDateLocal(int yyyy, int mm, int dd, int hh, int mn, int ss)
269     {
270         year = month = day = hours = minutes = seconds = -1;
271
272         if (yyyy > 0)
273         {
274             year = yyyy;
275         }
276         else
277         {
278             return;
279         }
280
281         if (mm > 0)
282         {
283             month = mm;
284         }
285         else
286         {
287             return;
288         }
289
290         if (dd > 0)
291         {
292             day = dd;
293         }
294         else
295         {
296             return;
297         }
298
299         if (hh == -1)
300         {
301             return;
302         }
303
304         // We have a time, so we need to do a timezone adjustment
305
localGC = new GregorianCalendar JavaDoc(year, month - 1, day, hh, mn, ss);
306
307         // Adjust to UTC
308
GregorianCalendar JavaDoc utcGC = new GregorianCalendar JavaDoc(TimeZone
309                 .getTimeZone("UTC"));
310
311         utcGC.setTime(localGC.getTime());
312
313         year = utcGC.get(Calendar.YEAR);
314
315         // Notation
316
month = utcGC.get(Calendar.MONTH) + 1;
317         day = utcGC.get(Calendar.DAY_OF_MONTH);
318         hours = utcGC.get(Calendar.HOUR_OF_DAY);
319         minutes = utcGC.get(Calendar.MINUTE);
320         seconds = utcGC.get(Calendar.SECOND);
321     }
322
323     /**
324      * Get the date as an array of ints, adjusted for the current timezone
325      *
326      * @return the date an an array: ( year, month, day, hour, minute, seconds) -
327      * unset fields are given a value of -1.
328      */

329     private int[] getDateLocal()
330     {
331         // Handle simple date cases first - no timezone adjustment
332
if (hours == -1)
333         {
334             return new int[] { year, month, day, -1, -1, -1 };
335         }
336
337         // We have a full time, adjust to current timezone
338
if (localGC == null)
339         {
340             GregorianCalendar JavaDoc utcGC = new GregorianCalendar JavaDoc(TimeZone
341                     .getTimeZone("UTC"));
342
343             utcGC.set(year, month - 1, day, hours, minutes, seconds);
344             localGC = new GregorianCalendar JavaDoc();
345             localGC.setTime(utcGC.getTime());
346         }
347
348         return new int[] { localGC.get(Calendar.YEAR),
349                 localGC.get(Calendar.MONTH) + 1,
350                 localGC.get(Calendar.DAY_OF_MONTH),
351                 localGC.get(Calendar.HOUR_OF_DAY),
352                 localGC.get(Calendar.MINUTE), localGC.get(Calendar.SECOND) };
353     }
354
355     /**
356      * Get the year, adjusting for current time zone.
357      *
358      * @return the year
359      */

360     public int getYear()
361     {
362         return (getDateLocal())[0];
363     }
364
365     /**
366      * Get the month, adjusting for current time zone.
367      *
368      * @return the month
369      */

370     public int getMonth()
371     {
372         return (getDateLocal())[1];
373     }
374
375     /**
376      * Get the day, adjusting for current time zone.
377      *
378      * @return the day
379      */

380     public int getDay()
381     {
382         return (getDateLocal())[2];
383     }
384
385     /**
386      * Get the hour, adjusting for current time zone.
387      *
388      * @return the hour
389      */

390     public int getHour()
391     {
392         return (getDateLocal())[3];
393     }
394
395     /**
396      * Get the minute, adjusting for current time zone.
397      *
398      * @return the minute
399      */

400     public int getMinute()
401     {
402         return (getDateLocal())[4];
403     }
404
405     /**
406      * Get the second, adjusting for current time zone.
407      *
408      * @return the second
409      */

410     public int getSecond()
411     {
412         return (getDateLocal())[5];
413     }
414
415     /**
416      * Get the date as an array of ints in GMT
417      *
418      * @return the date an an array: ( year, month, day, hour, minute, seconds) -
419      * unset fields are given a value of -1.
420      */

421     private int[] getDateGMT()
422     {
423         return new int[] { year, month, day, hours, minutes, seconds };
424     }
425
426     /**
427      * Get the year in GMT.
428      *
429      * @return the year
430      */

431     public int getYearGMT()
432     {
433         return (getDateGMT())[0];
434     }
435
436     /**
437      * Get the month in GMT.
438      *
439      * @return the month
440      */

441     public int getMonthGMT()
442     {
443         return (getDateGMT())[1];
444     }
445
446     /**
447      * Get the day in GMT.
448      *
449      * @return the day
450      */

451     public int getDayGMT()
452     {
453         return (getDateGMT())[2];
454     }
455
456     /**
457      * Get the hour in GMT.
458      *
459      * @return the hour
460      */

461     public int getHourGMT()
462     {
463         return (getDateGMT())[3];
464     }
465
466     /**
467      * Get the minute in GMT.
468      *
469      * @return the minute
470      */

471     public int getMinuteGMT()
472     {
473         return (getDateGMT())[4];
474     }
475
476     /**
477      * Get the second in GMT.
478      *
479      * @return the second
480      */

481     public int getSecondGMT()
482     {
483         return (getDateGMT())[5];
484     }
485
486     /**
487      * Flesh out a number to two digits
488      *
489      * @param n
490      * the number
491      * @return the number as a two-digit string
492      */

493     private String JavaDoc fleshOut(int n)
494     {
495         if (n < 10)
496         {
497             return "0" + n;
498         }
499         else
500         {
501             return String.valueOf(n);
502         }
503     }
504
505     /**
506      * Get a month's name for a month between 1 and 12. Any invalid month value
507      * (e.g. 0 or -1) will return a value of "Unspecified".
508      *
509      * @param m
510      * the month number
511      *
512      * @return the month name.
513      */

514     public static String JavaDoc getMonthName(int m)
515     {
516         if ((m > 0) && (m < 13))
517         {
518             return MONTHNAMES[m - 1];
519         }
520         else
521         {
522             return "Unspecified";
523         }
524     }
525 }
526
Popular Tags