]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
afc6f0acca17b51d75c8db3561de11ad2598a14a
[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.datetime;
19
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.GregorianCalendar;
24 import java.util.TimeZone;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  *
31  * GregorianCalendarDateTimeUtils.java
32  *
33  * $LastChangedRevision: $
34  * $LastChangedDate: $
35  *
36  */
37 public class GregorianCalendarDateTimeUtils {
38
39     private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
40
41     final static String UTC_TIMEZONE_IDENTIFIER = "UTC";
42     final static String ISO_8601_UTC_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
43
44    /**
45     * Returns the UTC time zone.
46     *
47     * @return The UTC time zone.  Defaults to the closely-related GMT time zone,
48     *         if for some reason the UTC time zone identifier cannot be understood.
49     */
50     public static TimeZone UTCTimeZone() {
51         return TimeZone.getTimeZone(UTC_TIMEZONE_IDENTIFIER);
52     }
53
54    /**
55     * Returns a calendar date, representing the current date and time instance
56     * in the UTC time zone.
57     *
58     * @return The current date and time instance in the UTC time zone.
59     */
60     public static GregorianCalendar currentDateAndTimeUTC() {
61         return currentDateAndTime(UTCTimeZone());
62     }
63
64    /**
65     * Returns a calendar date, representing the current date and time instance
66     * in the specified time zone.
67     *
68     * @return The current date and time instance in the specified time zone.
69     *         If the time zone is null, will return the current time and
70     *         date in the time zone intrinsic to a new Calendar instance.
71     */
72     public static GregorianCalendar currentDateAndTime(TimeZone tz) {
73         GregorianCalendar gcal = new GregorianCalendar();
74         if (tz != null) {
75             gcal.setTimeZone(tz);
76         }
77         Date now = new Date();
78         gcal.setTime(now);
79         return gcal;
80     }
81
82    /**
83     * Returns a String representing the current date and time instance.
84     * in the UTC time zone, formatted as an ISO 8601 timestamp.
85     *
86     * @return A String representing the current date and time instance.
87     */
88     public static String timestampUTC() {
89         return formatAsISO8601Timestamp(currentDateAndTime(UTCTimeZone()));
90     }
91
92    /**
93     * Returns a representation of a calendar date and time instance,
94     * as an ISO 8601-formatted timestamp in the UTC time zone.
95     *
96     * @param cal A calendar date and time instance
97     *
98     * @return    A representation of that calendar date and time instance,
99     *            as an ISO 8601-formatted timestamp in the UTC time zone.
100     */
101     public static String formatAsISO8601Timestamp(GregorianCalendar cal) {
102         return formatCalendarDate(cal, UTCTimeZone(), ISO8601TimestampFormatter());
103     }
104
105    /**
106     * Formats a provided calendar date using a provided date formatter,
107     * in the default system time zone.
108     *
109     * @param date  A calendar date to format.
110     * @param df    A date formatter to apply.
111     *
112     * @return      A formatted date string, or the empty string
113     *              if one or more of the parameter values were invalid.
114     */
115     public static String formatCalendarDate(GregorianCalendar gcal, DateFormat df) {
116         return formatCalendarDate(gcal, TimeZone.getDefault(), df);
117     }
118
119    /**
120     * Formats a provided calendar date using a provided date formatter,
121     * in a provided time zone.
122     *
123     * @param date  A calendar date to format.
124     * @param tz    The time zone qualifier for the calendar date to format.
125     * @param df    A date formatter to apply.
126     *
127     * @return      A formatted date string, or the empty string
128     *              if one or more of the parameter values were invalid.
129     */
130     public static String formatCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
131         String formattedDate = "";
132         if (gcal == null) {
133             logger.warn("Null calendar date was provided when a non-null calendar date was required.");
134             return formattedDate;
135         }
136         if (tz == null) {
137             logger.warn("Null time zone was provided when a non-null time zone was required.");
138             return formattedDate;
139         }
140         if (df == null) {
141             logger.warn("Null date formatter was provided when a non-null date formatter was required.");
142             return formattedDate;
143         }
144         gcal.setTimeZone(tz);
145         Date date = gcal.getTime();
146         df.setTimeZone(tz);
147         formattedDate = df.format(date);
148         return formattedDate;
149     }
150
151    /**
152     * Returns a date formatter for an ISO 8601 timestamp pattern.
153     *
154     * @return  A date formatter for an ISO 8601 timestamp pattern.
155     *          This pattern is specified as a class constant above.
156     */
157     public static DateFormat ISO8601TimestampFormatter() {
158         return getDateFormatter(ISO_8601_UTC_TIMESTAMP_PATTERN);
159     }
160
161    /**
162     * Returns a date formatter for a provided date or date/time pattern.
163     *
164     * @param pattern  A date or date/time pattern.
165     *
166     * @return         A date formatter using that pattern, or null
167     *                 if the pattern was null, empty, or invalid.
168     */
169     public static DateFormat getDateFormatter(String pattern) {
170         DateFormat df = null;
171         if (pattern == null || pattern.trim().isEmpty()) {
172             logger.warn("Null or empty date pattern string was provided " +
173                 "when a non-null, non-empty date pattern string was required.");
174             return df;
175         }
176         try {
177             df = new SimpleDateFormat(pattern);
178         } catch (IllegalArgumentException iae) {
179             logger.warn("Invalid date pattern string: " + pattern);
180         }
181         return df;
182     }
183
184 }