KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > time > DateFormatUtils


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.time;
17
18 import java.util.Date JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.TimeZone JavaDoc;
21
22 /**
23  * <p>Date and time formatting utilities and constants.</p>
24  *
25  * <p>Formatting is performed using the
26  * {@link org.apache.commons.lang.time.FastDateFormat} class.</p>
27  *
28  * @author Apache Ant - DateUtils
29  * @author <a HREF="mailto:sbailliez@apache.org">Stephane Bailliez</a>
30  * @author <a HREF="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
31  * @author Stephen Colebourne
32  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
33  * @since 2.0
34  * @version $Id: DateFormatUtils.java 161243 2005-04-14 04:30:28Z ggregory $
35  */

36 public class DateFormatUtils {
37
38     /**
39      * ISO8601 formatter for date-time without time zone.
40      * The format used is <tt>yyyy-MM-dd'T'HH:mm:ss</tt>.
41      */

42     public static final FastDateFormat ISO_DATETIME_FORMAT
43             = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss");
44
45     /**
46      * ISO8601 formatter for date-time with time zone.
47      * The format used is <tt>yyyy-MM-dd'T'HH:mm:ssZZ</tt>.
48      */

49     public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT
50             = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");
51
52     /**
53      * ISO8601 formatter for date without time zone.
54      * The format used is <tt>yyyy-MM-dd</tt>.
55      */

56     public static final FastDateFormat ISO_DATE_FORMAT
57             = FastDateFormat.getInstance("yyyy-MM-dd");
58
59     /**
60      * ISO8601-like formatter for date with time zone.
61      * The format used is <tt>yyyy-MM-ddZZ</tt>.
62      * This pattern does not comply with the formal ISO8601 specification
63      * as the standard does not allow a time zone without a time.
64      */

65     public static final FastDateFormat ISO_DATE_TIME_ZONE_FORMAT
66             = FastDateFormat.getInstance("yyyy-MM-ddZZ");
67
68     /**
69      * ISO8601 formatter for time without time zone.
70      * The format used is <tt>'T'HH:mm:ss</tt>.
71      */

72     public static final FastDateFormat ISO_TIME_FORMAT
73             = FastDateFormat.getInstance("'T'HH:mm:ss");
74
75     /**
76      * ISO8601 formatter for time with time zone.
77      * The format used is <tt>'T'HH:mm:ssZZ</tt>.
78      */

79     public static final FastDateFormat ISO_TIME_TIME_ZONE_FORMAT
80             = FastDateFormat.getInstance("'T'HH:mm:ssZZ");
81
82     /**
83      * ISO8601-like formatter for time without time zone.
84      * The format used is <tt>HH:mm:ss</tt>.
85      * This pattern does not comply with the formal ISO8601 specification
86      * as the standard requires the 'T' prefix for times.
87      */

88     public static final FastDateFormat ISO_TIME_NO_T_FORMAT
89             = FastDateFormat.getInstance("HH:mm:ss");
90
91     /**
92      * ISO8601-like formatter for time with time zone.
93      * The format used is <tt>HH:mm:ssZZ</tt>.
94      * This pattern does not comply with the formal ISO8601 specification
95      * as the standard requires the 'T' prefix for times.
96      */

97     public static final FastDateFormat ISO_TIME_NO_T_TIME_ZONE_FORMAT
98             = FastDateFormat.getInstance("HH:mm:ssZZ");
99
100     /**
101      * SMTP (and probably other) date headers.
102      * The format used is <tt>EEE, dd MMM yyyy HH:mm:ss Z</tt> in US locale.
103      */

104     public static final FastDateFormat SMTP_DATETIME_FORMAT
105             = FastDateFormat.getInstance("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
106
107     //-----------------------------------------------------------------------
108
/**
109      * <p>DateFormatUtils instances should NOT be constructed in standard programming.</p>
110      *
111      * <p>This constructor is public to permit tools that require a JavaBean instance
112      * to operate.</p>
113      */

114     public DateFormatUtils() {
115     }
116
117     /**
118      * <p>Format a date/time into a specific pattern using the UTC time zone.</p>
119      *
120      * @param millis the date to format expressed in milliseconds
121      * @param pattern the pattern to use to format the date
122      * @return the formatted date
123      */

124     public static String JavaDoc formatUTC(long millis, String JavaDoc pattern) {
125         return format(new Date JavaDoc(millis), pattern, DateUtils.UTC_TIME_ZONE, null);
126     }
127
128     /**
129      * <p>Format a date/time into a specific pattern using the UTC time zone.</p>
130      *
131      * @param date the date to format
132      * @param pattern the pattern to use to format the date
133      * @return the formatted date
134      */

135     public static String JavaDoc formatUTC(Date JavaDoc date, String JavaDoc pattern) {
136         return format(date, pattern, DateUtils.UTC_TIME_ZONE, null);
137     }
138     
139     /**
140      * <p>Format a date/time into a specific pattern using the UTC time zone.</p>
141      *
142      * @param millis the date to format expressed in milliseconds
143      * @param pattern the pattern to use to format the date
144      * @param locale the locale to use, may be <code>null</code>
145      * @return the formatted date
146      */

147     public static String JavaDoc formatUTC(long millis, String JavaDoc pattern, Locale JavaDoc locale) {
148         return format(new Date JavaDoc(millis), pattern, DateUtils.UTC_TIME_ZONE, locale);
149     }
150
151     /**
152      * <p>Format a date/time into a specific pattern using the UTC time zone.</p>
153      *
154      * @param date the date to format
155      * @param pattern the pattern to use to format the date
156      * @param locale the locale to use, may be <code>null</code>
157      * @return the formatted date
158      */

159     public static String JavaDoc formatUTC(Date JavaDoc date, String JavaDoc pattern, Locale JavaDoc locale) {
160         return format(date, pattern, DateUtils.UTC_TIME_ZONE, locale);
161     }
162     
163     /**
164      * <p>Format a date/time into a specific pattern.</p>
165      *
166      * @param millis the date to format expressed in milliseconds
167      * @param pattern the pattern to use to format the date
168      * @return the formatted date
169      */

170     public static String JavaDoc format(long millis, String JavaDoc pattern) {
171         return format(new Date JavaDoc(millis), pattern, null, null);
172     }
173
174     /**
175      * <p>Format a date/time into a specific pattern.</p>
176      *
177      * @param date the date to format
178      * @param pattern the pattern to use to format the date
179      * @return the formatted date
180      */

181     public static String JavaDoc format(Date JavaDoc date, String JavaDoc pattern) {
182         return format(date, pattern, null, null);
183     }
184     
185     /**
186      * <p>Format a date/time into a specific pattern in a time zone.</p>
187      *
188      * @param millis the time expressed in milliseconds
189      * @param pattern the pattern to use to format the date
190      * @param timeZone the time zone to use, may be <code>null</code>
191      * @return the formatted date
192      */

193     public static String JavaDoc format(long millis, String JavaDoc pattern, TimeZone JavaDoc timeZone) {
194         return format(new Date JavaDoc(millis), pattern, timeZone, null);
195     }
196
197     /**
198      * <p>Format a date/time into a specific pattern in a time zone.</p>
199      *
200      * @param date the date to format
201      * @param pattern the pattern to use to format the date
202      * @param timeZone the time zone to use, may be <code>null</code>
203      * @return the formatted date
204      */

205     public static String JavaDoc format(Date JavaDoc date, String JavaDoc pattern, TimeZone JavaDoc timeZone) {
206         return format(date, pattern, timeZone, null);
207     }
208
209     /**
210      * <p>Format a date/time into a specific pattern in a locale.</p>
211      *
212      * @param millis the date to format expressed in milliseconds
213      * @param pattern the pattern to use to format the date
214      * @param locale the locale to use, may be <code>null</code>
215      * @return the formatted date
216      */

217     public static String JavaDoc format(long millis, String JavaDoc pattern, Locale JavaDoc locale) {
218         return format(new Date JavaDoc(millis), pattern, null, locale);
219     }
220
221     /**
222      * <p>Format a date/time into a specific pattern in a locale.</p>
223      *
224      * @param date the date to format
225      * @param pattern the pattern to use to format the date
226      * @param locale the locale to use, may be <code>null</code>
227      * @return the formatted date
228      */

229     public static String JavaDoc format(Date JavaDoc date, String JavaDoc pattern, Locale JavaDoc locale) {
230         return format(date, pattern, null, locale);
231     }
232
233     /**
234      * <p>Format a date/time into a specific pattern in a time zone and locale.</p>
235      *
236      * @param millis the date to format expressed in milliseconds
237      * @param pattern the pattern to use to format the date
238      * @param timeZone the time zone to use, may be <code>null</code>
239      * @param locale the locale to use, may be <code>null</code>
240      * @return the formatted date
241      */

242     public static String JavaDoc format(long millis, String JavaDoc pattern, TimeZone JavaDoc timeZone, Locale JavaDoc locale) {
243         return format(new Date JavaDoc(millis), pattern, timeZone, locale);
244     }
245
246     /**
247      * <p>Format a date/time into a specific pattern in a time zone and locale.</p>
248      *
249      * @param date the date to format
250      * @param pattern the pattern to use to format the date
251      * @param timeZone the time zone to use, may be <code>null</code>
252      * @param locale the locale to use, may be <code>null</code>
253      * @return the formatted date
254      */

255     public static String JavaDoc format(Date JavaDoc date, String JavaDoc pattern, TimeZone JavaDoc timeZone, Locale JavaDoc locale) {
256         FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, locale);
257         return df.format(date);
258     }
259
260 }
261
Popular Tags