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.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;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
33 * GregorianCalendarDateTimeUtils.java
35 * $LastChangedRevision: $
39 public class GregorianCalendarDateTimeUtils {
41 private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
44 * Returns a String representing the current date and time instance.
45 * in the UTC time zone, formatted as an ISO 8601 timestamp.
47 * @return A String representing the current date and time instance.
49 public static String timestampUTC() {
50 return formatAsISO8601Timestamp(currentDateAndTime(DateUtils.UTCTimeZone()));
54 * Returns a String representing the current date and time instance.
55 * in the UTC time zone, formatted as an ISO 8601 date.
57 * @return A String representing the current date and time instance.
59 public static String currentDateUTC() {
60 return formatAsISO8601Date(currentDateAndTime(DateUtils.UTCTimeZone()));
64 * Returns a calendar date, representing the current date and time instance
65 * in the UTC time zone.
67 * @return The current date and time instance in the UTC time zone.
69 public static GregorianCalendar currentDateAndTimeUTC() {
70 return currentDateAndTime(DateUtils.UTCTimeZone());
74 * Returns a calendar date, representing the current date and time instance
75 * in the specified time zone.
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.
81 public static GregorianCalendar currentDateAndTime(TimeZone tz) {
82 GregorianCalendar gcal = new GregorianCalendar();
86 Date now = new Date();
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 formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
103 DateUtils.getDateFormatter(DateUtils.ISO_8601_UTC_TIMESTAMP_PATTERN));
107 * Returns a representation of a calendar date and time instance,
108 * as an ISO 8601-formatted date.
110 * @param cal a calendar date and time instance.
112 * @return a representation of that calendar date and time instance,
113 * as an ISO 8601-formatted date.
115 public static String formatAsISO8601Date(GregorianCalendar cal) {
116 return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
117 DateUtils.getDateFormatter(DateUtils.ISO_8601_DATE_PATTERN));
121 * Formats a provided calendar date using a supplied date formatter,
122 * in the default system time zone.
124 * @param date A 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 formatGregorianCalendarDate(GregorianCalendar gcal, DateFormat df) {
131 return formatGregorianCalendarDate(gcal, TimeZone.getDefault(), df);
135 * Formats a provided calendar date using a provided date formatter,
136 * in a provided time zone.
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.
142 * @return A formatted date string, or the empty string
143 * if one or more of the parameter values were invalid.
145 public static String formatGregorianCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
146 String formattedDate = "";
148 logger.warn("Null calendar date was provided when a non-null calendar date was required.");
149 return formattedDate;
152 logger.warn("Null time zone was provided when a non-null time zone was required.");
153 return formattedDate;
156 logger.warn("Null date formatter was provided when a non-null date formatter was required.");
157 return formattedDate;
159 gcal.setTimeZone(tz);
160 Date date = gcal.getTime();
162 formattedDate = df.format(date);
163 return formattedDate;