]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8fc7ee267166711c1a9608cf137ca584092490b9
[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 (c) 2009 Regents of the University of California
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  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  */
17 package org.collectionspace.services.client;
18
19 import java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22 import java.util.GregorianCalendar;
23 import java.util.TimeZone;
24
25 import javax.xml.datatype.DatatypeConfigurationException;
26 import javax.xml.datatype.DatatypeFactory;
27 import javax.xml.datatype.XMLGregorianCalendar;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * MovementClientDateTimeUtils.java
34  *
35  * $LastChangedRevision: 2107 $
36  * $LastChangedDate: 2010-05-17 18:22:27 -0700 (Mon, 17 May 2010) $
37  *
38  */
39 public class MovementClientDateTimeUtils {
40
41     private static final Logger logger =
42       LoggerFactory.getLogger(MovementClientDateTimeUtils.class);
43
44     final static String UTC_TIMEZONE_IDENTIFIER = "UTC";
45     final static String ISO_8601_UTC_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
46
47     // FIXME The methods below are not specific to the Movement service
48     // or its client code.
49     //
50     // At present, they may redundantly be included in or referenced from
51     // several classes in the Movement service module, in its 'service'
52     // and/or 'client' sub-modules.
53     //
54     // However, these methods, and any associated constants and imports
55     // above, should instead be moved to the Date and Time service or
56     // into another common package, where they can be shared by multiple services.
57
58    /**
59     * Returns the default time zone.
60     *
61     * @return The default time zone
62     */
63     public static TimeZone defaultTimeZone() {
64         return TimeZone.getDefault();
65     }
66
67    /**
68     * Returns a calendar date, representing the current date and time instance
69     * in the default time zone.
70     *
71     * @return The current date and time instance in the default time zone
72     */
73     public static GregorianCalendar currentDateAndTime() {
74         return currentDateAndTime(defaultTimeZone());
75     }
76
77    /**
78     * Returns the UTC time zone.
79     *
80     * @return The UTC time zone.  Defaults to the closely-related GMT time zone,
81     *         if for some reason the UTC time zone identifier cannot be understood.
82     */
83     public static TimeZone UTCTimeZone() {
84         return TimeZone.getTimeZone(UTC_TIMEZONE_IDENTIFIER);
85     }
86
87    /**
88     * Returns a calendar date, representing the current date and time instance
89     * in the UTC time zone.
90     *
91     * @return The current date and time instance in the UTC time zone.
92     */
93     public static GregorianCalendar currentDateAndTimeUTC() {
94         return currentDateAndTime(UTCTimeZone());
95     }
96
97    /**
98     * Returns a calendar date, representing the current date and time instance
99     * in the specified time zone.
100     *
101     * @return The current date and time instance in the specified time zone.
102     *         If the time zone is null, will return the current time and
103     *         date in the time zone intrinsic to a new Calendar instance.
104     */
105     public static GregorianCalendar currentDateAndTime(TimeZone tz) {
106         GregorianCalendar gcal = new GregorianCalendar();
107         if (tz != null) {
108             gcal.setTimeZone(tz);
109         }
110         Date now = new Date();
111         gcal.setTime(now);
112         return gcal;
113     }
114
115    /**
116     * Returns a String representing the current date and time instance.
117     * in the UTC time zone, formatted as an ISO 8601 timestamp.
118     *
119     * @return A String representing the current date and time instance.
120     */
121     public static String timestampUTC() {
122         return formatAsISO8601Timestamp(currentDateAndTime(UTCTimeZone()));
123     }
124
125    /**
126     * Returns a representation of a calendar date and time instance,
127     * as an ISO 8601-formatted timestamp in the UTC time zone.
128     *
129     * @param cal A calendar date and time instance
130     *
131     * @return    A representation of that calendar date and time instance,
132     *            as an ISO 8601-formatted timestamp in the UTC time zone.
133     */
134     public static String formatAsISO8601Timestamp(GregorianCalendar cal) {
135         return formatCalendarDate(cal, UTCTimeZone(), ISO8601TimestampFormatter());
136     }
137
138    /**
139     * Formats a provided calendar date using a provided date formatter,
140     * in the default system time zone.
141     *
142     * @param date  A calendar date to format.
143     * @param df    A date formatter to apply.
144     *
145     * @return      A formatted date string, or the empty string
146     *              if one or more of the parameter values were invalid.
147     */
148     public static String formatCalendarDate(GregorianCalendar gcal, DateFormat df) {
149         return formatCalendarDate(gcal, TimeZone.getDefault(), df);
150     }
151
152    /**
153     * Formats a provided calendar date using a provided date formatter,
154     * in a provided time zone.
155     *
156     * @param date  A calendar date to format.
157     * @param tz    The time zone qualifier for the calendar date to format.
158     * @param df    A date formatter to apply.
159     *
160     * @return      A formatted date string, or the empty string
161     *              if one or more of the parameter values were invalid.
162     */
163     public static String formatCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
164         String formattedDate = "";
165         if (gcal == null) {
166             logger.warn("Null calendar date was provided when a non-null calendar date was required.");
167             return formattedDate;
168         }
169         if (tz == null) {
170             logger.warn("Null time zone was provided when a non-null time zone was required.");
171             return formattedDate;
172         }
173         if (df == null) {
174             logger.warn("Null date formatter was provided when a non-null date formatter was required.");
175             return formattedDate;
176         }
177         gcal.setTimeZone(tz);
178         Date date = gcal.getTime();
179         df.setTimeZone(tz);
180         formattedDate = df.format(date);
181         return formattedDate;
182     }
183
184    /**
185     * Returns a date formatter for an ISO 8601 timestamp pattern.
186     *
187     * @return  A date formatter for an ISO 8601 timestamp pattern.
188     *          This pattern is specified as a class constant above.
189     */
190     public static DateFormat ISO8601TimestampFormatter() {
191         return getDateFormatter(ISO_8601_UTC_TIMESTAMP_PATTERN);
192     }
193
194    /**
195     * Returns a date formatter for a provided date or date/time pattern.
196     *
197     * @param pattern  A date or date/time pattern.
198     *
199     * @return         A date formatter using that pattern, or null
200     *                 if the pattern was null, empty, or invalid.
201     */
202     public static DateFormat getDateFormatter(String pattern) {
203         DateFormat df = null;
204         if (pattern == null || pattern.trim().isEmpty()) {
205             logger.warn("Null or empty date pattern string was provided " +
206                 "when a non-null, non-empty date pattern string was required.");
207             return df;
208         }
209         try {
210             df = new SimpleDateFormat(pattern);
211         } catch (IllegalArgumentException iae) {
212             logger.warn("Invalid date pattern string: " + pattern);
213         }
214         return df;
215     }
216 }