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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 package org.collectionspace.services.common.datetime;
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;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
31 * GregorianCalendarDateTimeUtils.java
33 * $LastChangedRevision: $
37 public class GregorianCalendarDateTimeUtils {
39 private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
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'";
45 * Returns the UTC time zone.
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.
50 public static TimeZone UTCTimeZone() {
51 return TimeZone.getTimeZone(UTC_TIMEZONE_IDENTIFIER);
55 * Returns a calendar date, representing the current date and time instance
56 * in the UTC time zone.
58 * @return The current date and time instance in the UTC time zone.
60 public static GregorianCalendar currentDateAndTimeUTC() {
61 return currentDateAndTime(UTCTimeZone());
65 * Returns a calendar date, representing the current date and time instance
66 * in the specified time zone.
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.
72 public static GregorianCalendar currentDateAndTime(TimeZone tz) {
73 GregorianCalendar gcal = new GregorianCalendar();
77 Date now = new Date();
83 * Returns a String representing the current date and time instance.
84 * in the UTC time zone, formatted as an ISO 8601 timestamp.
86 * @return A String representing the current date and time instance.
88 public static String timestampUTC() {
89 return formatAsISO8601Timestamp(currentDateAndTime(UTCTimeZone()));
93 * Returns a representation of a calendar date and time instance,
94 * as an ISO 8601-formatted timestamp in the UTC time zone.
96 * @param cal A calendar date and time instance
98 * @return A representation of that calendar date and time instance,
99 * as an ISO 8601-formatted timestamp in the UTC time zone.
101 public static String formatAsISO8601Timestamp(GregorianCalendar cal) {
102 return formatCalendarDate(cal, UTCTimeZone(), ISO8601TimestampFormatter());
106 * Formats a provided calendar date using a provided date formatter,
107 * in the default system time zone.
109 * @param date A calendar date to format.
110 * @param df A date formatter to apply.
112 * @return A formatted date string, or the empty string
113 * if one or more of the parameter values were invalid.
115 public static String formatCalendarDate(GregorianCalendar gcal, DateFormat df) {
116 return formatCalendarDate(gcal, TimeZone.getDefault(), df);
120 * Formats a provided calendar date using a provided date formatter,
121 * in a provided time zone.
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.
127 * @return A formatted date string, or the empty string
128 * if one or more of the parameter values were invalid.
130 public static String formatCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
131 String formattedDate = "";
133 logger.warn("Null calendar date was provided when a non-null calendar date was required.");
134 return formattedDate;
137 logger.warn("Null time zone was provided when a non-null time zone was required.");
138 return formattedDate;
141 logger.warn("Null date formatter was provided when a non-null date formatter was required.");
142 return formattedDate;
144 gcal.setTimeZone(tz);
145 Date date = gcal.getTime();
147 formattedDate = df.format(date);
148 return formattedDate;
152 * Returns a date formatter for an ISO 8601 timestamp pattern.
154 * @return A date formatter for an ISO 8601 timestamp pattern.
155 * This pattern is specified as a class constant above.
157 public static DateFormat ISO8601TimestampFormatter() {
158 return getDateFormatter(ISO_8601_UTC_TIMESTAMP_PATTERN);
162 * Returns a date formatter for a provided date or date/time pattern.
164 * @param pattern A date or date/time pattern.
166 * @return A date formatter using that pattern, or null
167 * if the pattern was null, empty, or invalid.
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.");
177 df = new SimpleDateFormat(pattern);
178 } catch (IllegalArgumentException iae) {
179 logger.warn("Invalid date pattern string: " + pattern);