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 (c) 2009 Regents of the University of California
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
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 package org.collectionspace.services.client;
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;
25 import javax.xml.datatype.DatatypeConfigurationException;
26 import javax.xml.datatype.DatatypeFactory;
27 import javax.xml.datatype.XMLGregorianCalendar;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * MovementClientDateTimeUtils.java
35 * $LastChangedRevision: 2107 $
36 * $LastChangedDate: 2010-05-17 18:22:27 -0700 (Mon, 17 May 2010) $
39 public class MovementClientDateTimeUtils {
41 private static final Logger logger =
42 LoggerFactory.getLogger(MovementClientDateTimeUtils.class);
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'";
47 // FIXME The methods below are not specific to the Movement service
48 // or its client code.
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.
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.
59 * Returns the default time zone.
61 * @return The default time zone
63 public static TimeZone defaultTimeZone() {
64 return TimeZone.getDefault();
68 * Returns a calendar date, representing the current date and time instance
69 * in the default time zone.
71 * @return The current date and time instance in the default time zone
73 public static GregorianCalendar currentDateAndTime() {
74 return currentDateAndTime(defaultTimeZone());
78 * Returns the UTC time zone.
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.
83 public static TimeZone UTCTimeZone() {
84 return TimeZone.getTimeZone(UTC_TIMEZONE_IDENTIFIER);
88 * Returns a calendar date, representing the current date and time instance
89 * in the UTC time zone.
91 * @return The current date and time instance in the UTC time zone.
93 public static GregorianCalendar currentDateAndTimeUTC() {
94 return currentDateAndTime(UTCTimeZone());
98 * Returns a calendar date, representing the current date and time instance
99 * in the specified time zone.
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.
105 public static GregorianCalendar currentDateAndTime(TimeZone tz) {
106 GregorianCalendar gcal = new GregorianCalendar();
108 gcal.setTimeZone(tz);
110 Date now = new Date();
116 * Returns a String representing the current date and time instance.
117 * in the UTC time zone, formatted as an ISO 8601 timestamp.
119 * @return A String representing the current date and time instance.
121 public static String timestampUTC() {
122 return formatAsISO8601Timestamp(currentDateAndTime(UTCTimeZone()));
126 * Returns a representation of a calendar date and time instance,
127 * as an ISO 8601-formatted timestamp in the UTC time zone.
129 * @param cal A calendar date and time instance
131 * @return A representation of that calendar date and time instance,
132 * as an ISO 8601-formatted timestamp in the UTC time zone.
134 public static String formatAsISO8601Timestamp(GregorianCalendar cal) {
135 return formatCalendarDate(cal, UTCTimeZone(), ISO8601TimestampFormatter());
139 * Formats a provided calendar date using a provided date formatter,
140 * in the default system time zone.
142 * @param date A calendar date to format.
143 * @param df A date formatter to apply.
145 * @return A formatted date string, or the empty string
146 * if one or more of the parameter values were invalid.
148 public static String formatCalendarDate(GregorianCalendar gcal, DateFormat df) {
149 return formatCalendarDate(gcal, TimeZone.getDefault(), df);
153 * Formats a provided calendar date using a provided date formatter,
154 * in a provided time zone.
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.
160 * @return A formatted date string, or the empty string
161 * if one or more of the parameter values were invalid.
163 public static String formatCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
164 String formattedDate = "";
166 logger.warn("Null calendar date was provided when a non-null calendar date was required.");
167 return formattedDate;
170 logger.warn("Null time zone was provided when a non-null time zone was required.");
171 return formattedDate;
174 logger.warn("Null date formatter was provided when a non-null date formatter was required.");
175 return formattedDate;
177 gcal.setTimeZone(tz);
178 Date date = gcal.getTime();
180 formattedDate = df.format(date);
181 return formattedDate;
185 * Returns a date formatter for an ISO 8601 timestamp pattern.
187 * @return A date formatter for an ISO 8601 timestamp pattern.
188 * This pattern is specified as a class constant above.
190 public static DateFormat ISO8601TimestampFormatter() {
191 return getDateFormatter(ISO_8601_UTC_TIMESTAMP_PATTERN);
195 * Returns a date formatter for a provided date or date/time pattern.
197 * @param pattern A date or date/time pattern.
199 * @return A date formatter using that pattern, or null
200 * if the pattern was null, empty, or invalid.
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.");
210 df = new SimpleDateFormat(pattern);
211 } catch (IllegalArgumentException iae) {
212 logger.warn("Invalid date pattern string: " + pattern);