]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
aa7979ed358b397c20d70e27d1560e98744eeaf6
[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.Calendar;
24 import java.util.Date;
25 import java.util.GregorianCalendar;
26 import java.util.Locale;
27 import java.util.TimeZone;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  *
34  * GregorianCalendarDateTimeUtils.java
35  *
36  * $LastChangedRevision: $
37  * $LastChangedDate: $
38  *
39  */
40 public class GregorianCalendarDateTimeUtils {
41
42     private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
43
44     /**
45      * Returns a String representing the current date and time instance.
46      * in the UTC time zone, formatted as an ISO 8601 timestamp.
47      *
48      * @return A String representing the current date and time instance.
49      */
50      public static String timestampUTC() {
51          return formatAsISO8601Timestamp(currentDateAndTime(DateUtils.UTCTimeZone()));
52      }
53      
54      //
55      // This is code take from the Nuxeo 6 code base.  It is stripping off the thousandths place of the milliseconds value.  I'm
56      // not sure why they are doing this.
57      //
58      public static String formatW3CDateTime(Date date) {
59          if (date == null) {
60              return null;
61          }
62          Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
63          cal.setTime(date);
64          StringBuilder buf = new StringBuilder(32);
65          return buf.append(cal.get(Calendar.YEAR)).append('-').append(
66                  pad(cal.get(Calendar.MONTH) + 1)).append('-').append(
67                  pad(cal.get(Calendar.DATE))).append('T').append(
68                  pad(cal.get(Calendar.HOUR_OF_DAY))).append(':').append(
69                  pad(cal.get(Calendar.MINUTE))).append(':').append(
70                  pad(cal.get(Calendar.SECOND))).append('.').append(
71                  pad(cal.get(Calendar.MILLISECOND) / 10)).append('Z').toString();
72      }
73      
74      private final static String pad(int i) {
75          return i < 10 ? "0".concat(String.valueOf(i)) : String.valueOf(i);
76      }
77      
78      
79     /**
80      * Returns a String representing the current date and time instance.
81      * in the UTC time zone, formatted as an ISO 8601 date.
82      *
83      * @return A String representing the current date and time instance.
84      */
85      public static String currentDateUTC() {
86          return formatAsISO8601Date(currentDateAndTime(DateUtils.UTCTimeZone()));
87      }
88     
89    /**
90     * Returns a calendar date, representing the current date and time instance
91     * in the UTC time zone.
92     *
93     * @return The current date and time instance in the UTC time zone.
94     */
95     public static GregorianCalendar currentDateAndTimeUTC() {
96         return currentDateAndTime(DateUtils.UTCTimeZone());
97     }
98
99    /**
100     * Returns a calendar date, representing the current date and time instance
101     * in the specified time zone.
102     *
103     * @return The current date and time instance in the specified time zone.
104     *         If the time zone is null, will return the current time and
105     *         date in the time zone intrinsic to a new Calendar instance.
106     */
107     public static GregorianCalendar currentDateAndTime(TimeZone tz) {
108         GregorianCalendar gcal = new GregorianCalendar();
109         if (tz != null) {
110             gcal.setTimeZone(tz);
111         }
112         Date now = new Date();
113         gcal.setTime(now);
114         return gcal;
115     }
116
117     
118     /**
119      * Returns a representation of a calendar date and time instance,
120      * as an ISO 8601-formatted timestamp in the UTC time zone.
121      *
122      * @param cal a calendar date and time instance.
123      *
124      * @return    a representation of that calendar date and time instance,
125      *            as an ISO 8601-formatted timestamp in the UTC time zone.
126      */
127     public static String formatAsISO8601Timestamp(GregorianCalendar cal) {
128         return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
129                         DateUtils.getDateFormatter(DateUtils.ISO_8601_UTC_TIMESTAMP_PATTERN));
130     }
131     
132     /**
133      * Returns a representation of a calendar date and time instance,
134      * as an ISO 8601-formatted date.
135      *
136      * @param cal a calendar date and time instance.
137      *
138      * @return    a representation of that calendar date and time instance,
139      *            as an ISO 8601-formatted date.
140      */
141     public static String formatAsISO8601Date(GregorianCalendar cal) {
142         return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
143                         DateUtils.getDateFormatter(DateUtils.ISO_8601_DATE_PATTERN));
144     }
145
146     /**
147      * Formats a provided calendar date using a supplied date formatter,
148      * in the default system time zone.
149      *
150      * @param date  A 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, DateFormat df) {
157         return formatGregorianCalendarDate(gcal, TimeZone.getDefault(), df);
158     }
159
160     /**
161      * Formats a provided calendar date using a provided date formatter,
162      * in a provided time zone.
163      *
164      * @param date  A calendar date to format.
165      * @param tz    The time zone qualifier for the calendar date to format.
166      * @param df    A date formatter to apply.
167      *
168      * @return      A formatted date string, or the empty string
169      *              if one or more of the parameter values were invalid.
170      */
171     public static String formatGregorianCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
172         String formattedDate = "";
173         if (gcal == null) {
174             logger.warn("Null calendar date was provided when a non-null calendar date was required.");
175             return formattedDate;
176         }
177         if (tz == null) {
178             logger.warn("Null time zone was provided when a non-null time zone was required.");
179             return formattedDate;
180         }
181         if (df == null) {
182             logger.warn("Null date formatter was provided when a non-null date formatter was required.");
183             return formattedDate;
184         }
185         gcal.setTimeZone(tz);
186         Date date = gcal.getTime();
187         df.setTimeZone(tz);
188         formattedDate = df.format(date);
189         return formattedDate;
190     }    
191 }