]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ec2217a0efae4b3268a638e871bf11a57cefeb5d
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright © 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17  */
18 package org.collectionspace.services.common.datetime;
19
20 import java.util.Date;
21 import java.util.GregorianCalendar;
22 import java.util.TimeZone;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  *
29  * GregorianCalendarDateTimeUtils.java
30  *
31  * $LastChangedRevision: $
32  * $LastChangedDate: $
33  *
34  */
35 public class GregorianCalendarDateTimeUtils {
36
37     private static final Logger logger = LoggerFactory.getLogger(GregorianCalendarDateTimeUtils.class);
38
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'";
41
42    /**
43     * Returns the UTC time zone.
44     *
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.
47     */
48     public static TimeZone UTCTimeZone() {
49         return TimeZone.getTimeZone(UTC_TIMEZONE_IDENTIFIER);
50     }
51
52    /**
53     * Returns a calendar date, representing the current date and time instance
54     * in the UTC time zone.
55     *
56     * @return The current date and time instance in the UTC time zone.
57     */
58     public static GregorianCalendar currentDateAndTimeUTC() {
59         return currentDateAndTime(UTCTimeZone());
60     }
61
62    /**
63     * Returns a calendar date, representing the current date and time instance
64     * in the specified time zone.
65     *
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.
69     */
70     public static GregorianCalendar currentDateAndTime(TimeZone tz) {
71         GregorianCalendar gcal = new GregorianCalendar();
72         if (tz != null) {
73             gcal.setTimeZone(tz);
74         }
75         Date now = new Date();
76         gcal.setTime(now);
77         return gcal;
78     }
79
80    /**
81     * Returns a String representing the current date and time instance.
82     * in the UTC time zone, formatted as an ISO 8601 timestamp.
83     *
84     * @return A String representing the current date and time instance.
85     */
86     public static String timestampUTC() {
87         return DateTimeFormatUtils.formatAsISO8601Timestamp(currentDateAndTime(UTCTimeZone()));
88     }
89     
90    /**
91     * Returns a String representing the current date and time instance.
92     * in the UTC time zone, formatted as an ISO 8601 date.
93     *
94     * @return A String representing the current date and time instance.
95     */
96     public static String currentDateUTC() {
97         return DateTimeFormatUtils.formatAsISO8601Date(currentDateAndTime(UTCTimeZone()));
98     }
99
100 }