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.Calendar;
24 import java.util.Date;
25 import java.util.GregorianCalendar;
26 import java.util.Locale;
27 import java.util.TimeZone;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
34 * GregorianCalendarDateTimeUtils.java
36 * $LastChangedRevision: $
40 public class GregorianCalendarDateTimeUtils {
42 private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
45 * Returns a String representing the current date and time instance.
46 * in the UTC time zone, formatted as an ISO 8601 timestamp.
48 * @return A String representing the current date and time instance.
50 public static String timestampUTC() {
51 return formatAsISO8601Timestamp(currentDateAndTime(DateUtils.UTCTimeZone()));
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.
58 public static String formatW3CDateTime(Date date) {
62 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
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();
74 private final static String pad(int i) {
75 return i < 10 ? "0".concat(String.valueOf(i)) : String.valueOf(i);
80 * Returns a String representing the current date and time instance.
81 * in the UTC time zone, formatted as an ISO 8601 date.
83 * @return A String representing the current date and time instance.
85 public static String currentDateUTC() {
86 return formatAsISO8601Date(currentDateAndTime(DateUtils.UTCTimeZone()));
90 * Returns a calendar date, representing the current date and time instance
91 * in the UTC time zone.
93 * @return The current date and time instance in the UTC time zone.
95 public static GregorianCalendar currentDateAndTimeUTC() {
96 return currentDateAndTime(DateUtils.UTCTimeZone());
100 * Returns a calendar date, representing the current date and time instance
101 * in the specified time zone.
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.
107 public static GregorianCalendar currentDateAndTime(TimeZone tz) {
108 GregorianCalendar gcal = new GregorianCalendar();
110 gcal.setTimeZone(tz);
112 Date now = new Date();
119 * Returns a representation of a calendar date and time instance,
120 * as an ISO 8601-formatted timestamp in the UTC time zone.
122 * @param cal a calendar date and time instance.
124 * @return a representation of that calendar date and time instance,
125 * as an ISO 8601-formatted timestamp in the UTC time zone.
127 public static String formatAsISO8601Timestamp(GregorianCalendar cal) {
128 return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
129 DateUtils.getDateFormatter(DateUtils.ISO_8601_UTC_TIMESTAMP_PATTERN));
133 * Returns a representation of a calendar date and time instance,
134 * as an ISO 8601-formatted date.
136 * @param cal a calendar date and time instance.
138 * @return a representation of that calendar date and time instance,
139 * as an ISO 8601-formatted date.
141 public static String formatAsISO8601Date(GregorianCalendar cal) {
142 return formatGregorianCalendarDate(cal, DateUtils.UTCTimeZone(),
143 DateUtils.getDateFormatter(DateUtils.ISO_8601_DATE_PATTERN));
147 * Formats a provided calendar date using a supplied date formatter,
148 * in the default system time zone.
150 * @param date A 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, DateFormat df) {
157 return formatGregorianCalendarDate(gcal, TimeZone.getDefault(), df);
161 * Formats a provided calendar date using a provided date formatter,
162 * in a provided time zone.
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.
168 * @return A formatted date string, or the empty string
169 * if one or more of the parameter values were invalid.
171 public static String formatGregorianCalendarDate(GregorianCalendar gcal, TimeZone tz, DateFormat df) {
172 String formattedDate = "";
174 logger.warn("Null calendar date was provided when a non-null calendar date was required.");
175 return formattedDate;
178 logger.warn("Null time zone was provided when a non-null time zone was required.");
179 return formattedDate;
182 logger.warn("Null date formatter was provided when a non-null date formatter was required.");
183 return formattedDate;
185 gcal.setTimeZone(tz);
186 Date date = gcal.getTime();
188 formattedDate = df.format(date);
189 return formattedDate;