]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1f0af89702051c72e35cac038b3bfc3890bc0dce
[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.text.ParseException;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.Locale;
26 import java.util.TimeZone;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  *
33  * GregorianCalendarDateTimeUtils.java
34  *
35  * $LastChangedRevision: $
36  * $LastChangedDate: $
37  *
38  */
39 public class GregorianCalendarDateTimeUtils {
40
41     private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
42
43     /**
44      * Returns a String representing the current date and time instance.
45      * in the UTC time zone, formatted as an ISO 8601 timestamp.
46      *
47      * @return A String representing the current date and time instance.
48      */
49      public static String timestampUTC() {
50          return formatAsISO8601Timestamp(currentDateAndTime(DateUtils.UTCTimeZone()));
51      }
52      
53     /**
54      * Returns a String representing the current date and time instance.
55      * in the UTC time zone, formatted as an ISO 8601 date.
56      *
57      * @return A String representing the current date and time instance.
58      */
59      public static String currentDateUTC() {
60          return formatAsISO8601Date(currentDateAndTime(DateUtils.UTCTimeZone()));
61      }
62     
63    /**
64     * Returns a calendar date, representing the current date and time instance
65     * in the UTC time zone.
66     *
67     * @return The current date and time instance in the UTC time zone.
68     */
69     public static GregorianCalendar currentDateAndTimeUTC() {
70         return currentDateAndTime(DateUtils.UTCTimeZone());
71     }
72
73    /**
74     * Returns a calendar date, representing the current date and time instance
75     * in the specified time zone.
76     *
77     * @return The current date and time instance in the specified time zone.
78     *         If the time zone is null, will return the current time and
79     *         date in the time zone intrinsic to a new Calendar instance.
80     */
81     public static GregorianCalendar currentDateAndTime(TimeZone tz) {
82         GregorianCalendar gcal = new GregorianCalendar();
83         if (tz != null) {
84             gcal.setTimeZone(tz);
85         }
86         Date now = new Date();
87         gcal.setTime(now);
88         return gcal;
89     }
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 formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
103                         DateUtils.getDateFormatter(DateUtils.ISO_8601_UTC_TIMESTAMP_PATTERN));
104     }
105     
106     /**
107      * Returns a representation of a calendar date and time instance,
108      * as an ISO 8601-formatted date.
109      *
110      * @param cal a calendar date and time instance.
111      *
112      * @return    a representation of that calendar date and time instance,
113      *            as an ISO 8601-formatted date.
114      */
115     public static String formatAsISO8601Date(GregorianCalendar cal) {
116         return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
117                         DateUtils.getDateFormatter(DateUtils.ISO_8601_DATE_PATTERN));
118     }
119
120     /**
121      * Formats a provided calendar date using a supplied date formatter,
122      * in the default system time zone.
123      *
124      * @param date  A 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 formatGregorianCalendarDate(GregorianCalendar gcal, DateFormat df) {
131         return formatGregorianCalendarDate(gcal, TimeZone.getDefault(), df);
132     }
133
134     /**
135      * Formats a provided calendar date using a provided date formatter,
136      * in a provided time zone.
137      *
138      * @param date  A calendar date to format.
139      * @param tz    The time zone qualifier for the calendar date to format.
140      * @param df    A date formatter to apply.
141      *
142      * @return      A formatted date string, or the empty string
143      *              if one or more of the parameter values were invalid.
144      */
145     public static String formatGregorianCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
146         String formattedDate = "";
147         if (gcal == null) {
148             logger.warn("Null calendar date was provided when a non-null calendar date was required.");
149             return formattedDate;
150         }
151         if (tz == null) {
152             logger.warn("Null time zone was provided when a non-null time zone was required.");
153             return formattedDate;
154         }
155         if (df == null) {
156             logger.warn("Null date formatter was provided when a non-null date formatter was required.");
157             return formattedDate;
158         }
159         gcal.setTimeZone(tz);
160         Date date = gcal.getTime();
161         df.setTimeZone(tz);
162         formattedDate = df.format(date);
163         return formattedDate;
164     }    
165 }