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.util.Date;
21 import java.util.GregorianCalendar;
22 import java.util.TimeZone;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
29 * GregorianCalendarDateTimeUtils.java
31 * $LastChangedRevision: $
35 public class GregorianCalendarDateTimeUtils {
37 private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
39 final static String UTC_TIMEZONE_IDENTIFIER = "UTC";
40 final static String ISO_8601_UTC_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
43 * Returns the UTC time zone.
45 * @return The UTC time zone. Defaults to the closely-related GMT time zone,
46 * if for some reason the UTC time zone identifier cannot be understood.
48 public static TimeZone UTCTimeZone() {
49 return TimeZone.getTimeZone(UTC_TIMEZONE_IDENTIFIER);
53 * Returns a calendar date, representing the current date and time instance
54 * in the UTC time zone.
56 * @return The current date and time instance in the UTC time zone.
58 public static GregorianCalendar currentDateAndTimeUTC() {
59 return currentDateAndTime(UTCTimeZone());
63 * Returns a calendar date, representing the current date and time instance
64 * in the specified time zone.
66 * @return The current date and time instance in the specified time zone.
67 * If the time zone is null, will return the current time and
68 * date in the time zone intrinsic to a new Calendar instance.
70 public static GregorianCalendar currentDateAndTime(TimeZone tz) {
71 GregorianCalendar gcal = new GregorianCalendar();
75 Date now = new Date();
81 * Returns a String representing the current date and time instance.
82 * in the UTC time zone, formatted as an ISO 8601 timestamp.
84 * @return A String representing the current date and time instance.
86 public static String timestampUTC() {
87 return DateTimeFormatUtils.formatAsISO8601Timestamp(currentDateAndTime(UTCTimeZone()));
91 * Returns a String representing the current date and time instance.
92 * in the UTC time zone, formatted as an ISO 8601 date.
94 * @return A String representing the current date and time instance.
96 public static String currentDateUTC() {
97 return DateTimeFormatUtils.formatAsISO8601Date(currentDateAndTime(UTCTimeZone()));