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.api;
20 import java.text.DateFormat;
21 import java.util.Date;
22 import java.util.GregorianCalendar;
23 import java.util.TimeZone;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
30 * GregorianCalendarDateTimeUtils.java
32 * $LastChangedRevision: $
36 public class GregorianCalendarDateTimeUtils {
38 private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
41 * Returns a String representing the current date and time instance.
42 * in the UTC time zone, formatted as an ISO 8601 timestamp.
44 * @return A String representing the current date and time instance.
46 public static String timestampUTC() {
47 return formatAsISO8601Timestamp(currentDateAndTime(DateUtils.UTCTimeZone()));
51 * Returns a String representing the current date and time instance.
52 * in the UTC time zone, formatted as an ISO 8601 date.
54 * @return A String representing the current date and time instance.
56 public static String currentDateUTC() {
57 return formatAsISO8601Date(currentDateAndTime(DateUtils.UTCTimeZone()));
61 * Returns a calendar date, representing the current date and time instance
62 * in the UTC time zone.
64 * @return The current date and time instance in the UTC time zone.
66 public static GregorianCalendar currentDateAndTimeUTC() {
67 return currentDateAndTime(DateUtils.UTCTimeZone());
71 * Returns a calendar date, representing the current date and time instance
72 * in the specified time zone.
74 * @return The current date and time instance in the specified time zone.
75 * If the time zone is null, will return the current time and
76 * date in the time zone intrinsic to a new Calendar instance.
78 public static GregorianCalendar currentDateAndTime(TimeZone tz) {
79 GregorianCalendar gcal = new GregorianCalendar();
83 Date now = new Date();
89 * Returns a representation of a calendar date
90 * as an ISO 8601-formatted timestamp in the UTC time zone.
92 * @param cal a calendar date and time instance.
94 * @return a representation of that calendar date and time instance,
95 * as an ISO 8601-formatted timestamp in the UTC time zone.
97 public static String formatAsISO8601Timestamp(Date date) {
98 GregorianCalendar gcal = new GregorianCalendar();
100 return formatAsISO8601Timestamp(gcal);
104 * Returns a representation of a calendar date and time instance,
105 * as an ISO 8601-formatted timestamp in the UTC time zone.
107 * @param cal a calendar date and time instance.
109 * @return a representation of that calendar date and time instance,
110 * as an ISO 8601-formatted timestamp in the UTC time zone.
112 public static String formatAsISO8601Timestamp(GregorianCalendar cal) {
113 return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
114 DateUtils.getDateFormatter(DateUtils.ISO_8601_UTC_TIMESTAMP_PATTERN));
118 * Returns a representation of a calendar date and time instance,
119 * as an ISO 8601-formatted date.
121 * @param cal a calendar date and time instance.
123 * @return a representation of that calendar date and time instance,
124 * as an ISO 8601-formatted date.
126 public static String formatAsISO8601Date(GregorianCalendar cal) {
127 return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
128 DateUtils.getDateFormatter(DateUtils.ISO_8601_DATE_PATTERN));
132 * Formats a provided calendar date using a supplied date formatter,
133 * in the default system time zone.
135 * @param date A calendar date to format.
136 * @param df A date formatter to apply.
138 * @return A formatted date string, or the empty string
139 * if one or more of the parameter values were invalid.
141 public static String formatGregorianCalendarDate(GregorianCalendar gcal, DateFormat df) {
142 return formatGregorianCalendarDate(gcal, TimeZone.getDefault(), df);
146 * Formats a provided calendar date using a provided date formatter,
147 * in a provided time zone.
149 * @param date A calendar date to format.
150 * @param tz The time zone qualifier for the calendar date to format.
151 * @param df A date formatter to apply.
153 * @return A formatted date string, or the empty string
154 * if one or more of the parameter values were invalid.
156 public static String formatGregorianCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
157 String formattedDate = "";
159 logger.warn("Null calendar date was provided when a non-null calendar date was required.");
160 return formattedDate;
163 logger.warn("Null time zone was provided when a non-null time zone was required.");
164 return formattedDate;
167 logger.warn("Null date formatter was provided when a non-null date formatter was required.");
168 return formattedDate;
170 gcal.setTimeZone(tz);
171 Date date = gcal.getTime();
173 formattedDate = df.format(date);
174 return formattedDate;