]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5ebca7b5ddeabcaf9abec4a809e987bd0d3e1524
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright © 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17  */
18 package org.collectionspace.services.common.api;
19
20 import java.text.DateFormat;
21 import java.util.Date;
22 import java.util.GregorianCalendar;
23 import java.util.TimeZone;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  *
30  * GregorianCalendarDateTimeUtils.java
31  *
32  * $LastChangedRevision: $
33  * $LastChangedDate: $
34  *
35  */
36 public class GregorianCalendarDateTimeUtils {
37
38     private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
39
40     /**
41      * Returns a String representing the current date and time instance.
42      * in the UTC time zone, formatted as an ISO 8601 timestamp.
43      *
44      * @return A String representing the current date and time instance.
45      */
46      public static String timestampUTC() {
47          return formatAsISO8601Timestamp(currentDateAndTime(DateUtils.UTCTimeZone()));
48      }     
49      
50     /**
51      * Returns a String representing the current date and time instance.
52      * in the UTC time zone, formatted as an ISO 8601 date.
53      *
54      * @return A String representing the current date and time instance.
55      */
56      public static String currentDateUTC() {
57          return formatAsISO8601Date(currentDateAndTime(DateUtils.UTCTimeZone()));
58      }
59     
60    /**
61     * Returns a calendar date, representing the current date and time instance
62     * in the UTC time zone.
63     *
64     * @return The current date and time instance in the UTC time zone.
65     */
66     public static GregorianCalendar currentDateAndTimeUTC() {
67         return currentDateAndTime(DateUtils.UTCTimeZone());
68     }
69
70    /**
71     * Returns a calendar date, representing the current date and time instance
72     * in the specified time zone.
73     *
74     * @return The current date and time instance in the specified time zone.
75     *         If the time zone is null, will return the current time and
76     *         date in the time zone intrinsic to a new Calendar instance.
77     */
78     public static GregorianCalendar currentDateAndTime(TimeZone tz) {
79         GregorianCalendar gcal = new GregorianCalendar();
80         if (tz != null) {
81             gcal.setTimeZone(tz);
82         }
83         Date now = new Date();
84         gcal.setTime(now);
85         return gcal;
86     }
87
88     /**
89      * Returns a representation of a calendar date
90      * as an ISO 8601-formatted timestamp in the UTC time zone.
91      *
92      * @param cal a calendar date and time instance.
93      *
94      * @return    a representation of that calendar date and time instance,
95      *            as an ISO 8601-formatted timestamp in the UTC time zone.
96      */
97     public static String formatAsISO8601Timestamp(Date date) {
98         GregorianCalendar gcal = new GregorianCalendar();
99         gcal.setTime(date);
100         return formatAsISO8601Timestamp(gcal);
101     }
102     
103     /**
104      * Returns a representation of a calendar date and time instance,
105      * as an ISO 8601-formatted timestamp in the UTC time zone.
106      *
107      * @param cal a calendar date and time instance.
108      *
109      * @return    a representation of that calendar date and time instance,
110      *            as an ISO 8601-formatted timestamp in the UTC time zone.
111      */
112     public static String formatAsISO8601Timestamp(GregorianCalendar cal) {
113         return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
114                         DateUtils.getDateFormatter(DateUtils.ISO_8601_UTC_TIMESTAMP_PATTERN));
115     }
116     
117     /**
118      * Returns a representation of a calendar date and time instance,
119      * as an ISO 8601-formatted date.
120      *
121      * @param cal a calendar date and time instance.
122      *
123      * @return    a representation of that calendar date and time instance,
124      *            as an ISO 8601-formatted date.
125      */
126     public static String formatAsISO8601Date(GregorianCalendar cal) {
127         return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
128                         DateUtils.getDateFormatter(DateUtils.ISO_8601_DATE_PATTERN));
129     }
130
131     /**
132      * Formats a provided calendar date using a supplied date formatter,
133      * in the default system time zone.
134      *
135      * @param date  A calendar date to format.
136      * @param df    A date formatter to apply.
137      *
138      * @return      A formatted date string, or the empty string
139      *              if one or more of the parameter values were invalid.
140      */
141     public static String formatGregorianCalendarDate(GregorianCalendar gcal, DateFormat df) {
142         return formatGregorianCalendarDate(gcal, TimeZone.getDefault(), df);
143     }
144
145     /**
146      * Formats a provided calendar date using a provided date formatter,
147      * in a provided time zone.
148      *
149      * @param date  A calendar date to format.
150      * @param tz    The time zone qualifier for the calendar date to format.
151      * @param df    A date formatter to apply.
152      *
153      * @return      A formatted date string, or the empty string
154      *              if one or more of the parameter values were invalid.
155      */
156     public static String formatGregorianCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
157         String formattedDate = "";
158         if (gcal == null) {
159             logger.warn("Null calendar date was provided when a non-null calendar date was required.");
160             return formattedDate;
161         }
162         if (tz == null) {
163             logger.warn("Null time zone was provided when a non-null time zone was required.");
164             return formattedDate;
165         }
166         if (df == null) {
167             logger.warn("Null date formatter was provided when a non-null date formatter was required.");
168             return formattedDate;
169         }
170         gcal.setTimeZone(tz);
171         Date date = gcal.getTime();
172         df.setTimeZone(tz);
173         formattedDate = df.format(date);
174         return formattedDate;
175     }    
176 }