001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang.time;
018:
019: import java.util.Date;
020: import java.util.Locale;
021: import java.util.TimeZone;
022:
023: /**
024: * <p>Date and time formatting utilities and constants.</p>
025: *
026: * <p>Formatting is performed using the
027: * {@link org.apache.commons.lang.time.FastDateFormat} class.</p>
028: *
029: * @author Apache Ant - DateUtils
030: * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
031: * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
032: * @author Stephen Colebourne
033: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
034: * @since 2.0
035: * @version $Id: DateFormatUtils.java 437554 2006-08-28 06:21:41Z bayard $
036: */
037: public class DateFormatUtils {
038:
039: /**
040: * ISO8601 formatter for date-time without time zone.
041: * The format used is <tt>yyyy-MM-dd'T'HH:mm:ss</tt>.
042: */
043: public static final FastDateFormat ISO_DATETIME_FORMAT = FastDateFormat
044: .getInstance("yyyy-MM-dd'T'HH:mm:ss");
045:
046: /**
047: * ISO8601 formatter for date-time with time zone.
048: * The format used is <tt>yyyy-MM-dd'T'HH:mm:ssZZ</tt>.
049: */
050: public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT = FastDateFormat
051: .getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");
052:
053: /**
054: * ISO8601 formatter for date without time zone.
055: * The format used is <tt>yyyy-MM-dd</tt>.
056: */
057: public static final FastDateFormat ISO_DATE_FORMAT = FastDateFormat
058: .getInstance("yyyy-MM-dd");
059:
060: /**
061: * ISO8601-like formatter for date with time zone.
062: * The format used is <tt>yyyy-MM-ddZZ</tt>.
063: * This pattern does not comply with the formal ISO8601 specification
064: * as the standard does not allow a time zone without a time.
065: */
066: public static final FastDateFormat ISO_DATE_TIME_ZONE_FORMAT = FastDateFormat
067: .getInstance("yyyy-MM-ddZZ");
068:
069: /**
070: * ISO8601 formatter for time without time zone.
071: * The format used is <tt>'T'HH:mm:ss</tt>.
072: */
073: public static final FastDateFormat ISO_TIME_FORMAT = FastDateFormat
074: .getInstance("'T'HH:mm:ss");
075:
076: /**
077: * ISO8601 formatter for time with time zone.
078: * The format used is <tt>'T'HH:mm:ssZZ</tt>.
079: */
080: public static final FastDateFormat ISO_TIME_TIME_ZONE_FORMAT = FastDateFormat
081: .getInstance("'T'HH:mm:ssZZ");
082:
083: /**
084: * ISO8601-like formatter for time without time zone.
085: * The format used is <tt>HH:mm:ss</tt>.
086: * This pattern does not comply with the formal ISO8601 specification
087: * as the standard requires the 'T' prefix for times.
088: */
089: public static final FastDateFormat ISO_TIME_NO_T_FORMAT = FastDateFormat
090: .getInstance("HH:mm:ss");
091:
092: /**
093: * ISO8601-like formatter for time with time zone.
094: * The format used is <tt>HH:mm:ssZZ</tt>.
095: * This pattern does not comply with the formal ISO8601 specification
096: * as the standard requires the 'T' prefix for times.
097: */
098: public static final FastDateFormat ISO_TIME_NO_T_TIME_ZONE_FORMAT = FastDateFormat
099: .getInstance("HH:mm:ssZZ");
100:
101: /**
102: * SMTP (and probably other) date headers.
103: * The format used is <tt>EEE, dd MMM yyyy HH:mm:ss Z</tt> in US locale.
104: */
105: public static final FastDateFormat SMTP_DATETIME_FORMAT = FastDateFormat
106: .getInstance("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
107:
108: //-----------------------------------------------------------------------
109: /**
110: * <p>DateFormatUtils instances should NOT be constructed in standard programming.</p>
111: *
112: * <p>This constructor is public to permit tools that require a JavaBean instance
113: * to operate.</p>
114: */
115: public DateFormatUtils() {
116: super ();
117: }
118:
119: /**
120: * <p>Formats a date/time into a specific pattern using the UTC time zone.</p>
121: *
122: * @param millis the date to format expressed in milliseconds
123: * @param pattern the pattern to use to format the date
124: * @return the formatted date
125: */
126: public static String formatUTC(long millis, String pattern) {
127: return format(new Date(millis), pattern,
128: DateUtils.UTC_TIME_ZONE, null);
129: }
130:
131: /**
132: * <p>Formats a date/time into a specific pattern using the UTC time zone.</p>
133: *
134: * @param date the date to format
135: * @param pattern the pattern to use to format the date
136: * @return the formatted date
137: */
138: public static String formatUTC(Date date, String pattern) {
139: return format(date, pattern, DateUtils.UTC_TIME_ZONE, null);
140: }
141:
142: /**
143: * <p>Formats a date/time into a specific pattern using the UTC time zone.</p>
144: *
145: * @param millis the date to format expressed in milliseconds
146: * @param pattern the pattern to use to format the date
147: * @param locale the locale to use, may be <code>null</code>
148: * @return the formatted date
149: */
150: public static String formatUTC(long millis, String pattern,
151: Locale locale) {
152: return format(new Date(millis), pattern,
153: DateUtils.UTC_TIME_ZONE, locale);
154: }
155:
156: /**
157: * <p>Formats a date/time into a specific pattern using the UTC time zone.</p>
158: *
159: * @param date the date to format
160: * @param pattern the pattern to use to format the date
161: * @param locale the locale to use, may be <code>null</code>
162: * @return the formatted date
163: */
164: public static String formatUTC(Date date, String pattern,
165: Locale locale) {
166: return format(date, pattern, DateUtils.UTC_TIME_ZONE, locale);
167: }
168:
169: /**
170: * <p>Formats a date/time into a specific pattern.</p>
171: *
172: * @param millis the date to format expressed in milliseconds
173: * @param pattern the pattern to use to format the date
174: * @return the formatted date
175: */
176: public static String format(long millis, String pattern) {
177: return format(new Date(millis), pattern, null, null);
178: }
179:
180: /**
181: * <p>Formats a date/time into a specific pattern.</p>
182: *
183: * @param date the date to format
184: * @param pattern the pattern to use to format the date
185: * @return the formatted date
186: */
187: public static String format(Date date, String pattern) {
188: return format(date, pattern, null, null);
189: }
190:
191: /**
192: * <p>Formats a date/time into a specific pattern in a time zone.</p>
193: *
194: * @param millis the time expressed in milliseconds
195: * @param pattern the pattern to use to format the date
196: * @param timeZone the time zone to use, may be <code>null</code>
197: * @return the formatted date
198: */
199: public static String format(long millis, String pattern,
200: TimeZone timeZone) {
201: return format(new Date(millis), pattern, timeZone, null);
202: }
203:
204: /**
205: * <p>Formats a date/time into a specific pattern in a time zone.</p>
206: *
207: * @param date the date to format
208: * @param pattern the pattern to use to format the date
209: * @param timeZone the time zone to use, may be <code>null</code>
210: * @return the formatted date
211: */
212: public static String format(Date date, String pattern,
213: TimeZone timeZone) {
214: return format(date, pattern, timeZone, null);
215: }
216:
217: /**
218: * <p>Formats a date/time into a specific pattern in a locale.</p>
219: *
220: * @param millis the date to format expressed in milliseconds
221: * @param pattern the pattern to use to format the date
222: * @param locale the locale to use, may be <code>null</code>
223: * @return the formatted date
224: */
225: public static String format(long millis, String pattern,
226: Locale locale) {
227: return format(new Date(millis), pattern, null, locale);
228: }
229:
230: /**
231: * <p>Formats a date/time into a specific pattern in a locale.</p>
232: *
233: * @param date the date to format
234: * @param pattern the pattern to use to format the date
235: * @param locale the locale to use, may be <code>null</code>
236: * @return the formatted date
237: */
238: public static String format(Date date, String pattern, Locale locale) {
239: return format(date, pattern, null, locale);
240: }
241:
242: /**
243: * <p>Formats a date/time into a specific pattern in a time zone and locale.</p>
244: *
245: * @param millis the date to format expressed in milliseconds
246: * @param pattern the pattern to use to format the date
247: * @param timeZone the time zone to use, may be <code>null</code>
248: * @param locale the locale to use, may be <code>null</code>
249: * @return the formatted date
250: */
251: public static String format(long millis, String pattern,
252: TimeZone timeZone, Locale locale) {
253: return format(new Date(millis), pattern, timeZone, locale);
254: }
255:
256: /**
257: * <p>Formats a date/time into a specific pattern in a time zone and locale.</p>
258: *
259: * @param date the date to format
260: * @param pattern the pattern to use to format the date
261: * @param timeZone the time zone to use, may be <code>null</code>
262: * @param locale the locale to use, may be <code>null</code>
263: * @return the formatted date
264: */
265: public static String format(Date date, String pattern,
266: TimeZone timeZone, Locale locale) {
267: FastDateFormat df = FastDateFormat.getInstance(pattern,
268: timeZone, locale);
269: return df.format(date);
270: }
271:
272: }
|