KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > DateUtil


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.util;
20
21 import java.text.ParseException JavaDoc;
22 import java.text.ParsePosition JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.TimeZone JavaDoc;
28 import org.apache.commons.lang.StringUtils;
29
30
31 /**
32  * General purpose date utilities.
33  */

34 public abstract class DateUtil {
35     
36     public static final long millisInDay = 86400000;
37     
38     // a bunch of date formats
39
private static final String JavaDoc formatDefaultDate = "dd.MM.yyyy";
40     private static final String JavaDoc formatDefaultDateMinimal = "d.M.yy";
41     private static final String JavaDoc formatDefaultTimestamp = "yyyy-MM-dd HH:mm:ss.SSS";
42     
43     private static final String JavaDoc formatFriendlyTimestamp = "dd.MM.yyyy HH:mm:ss";
44     
45     private static final String JavaDoc format6chars = "yyyyMM";
46     private static final String JavaDoc format8chars = "yyyyMMdd";
47     
48     private static final String JavaDoc formatIso8601 = "yyyy-MM-dd'T'HH:mm:ssZ";
49     private static final String JavaDoc formatIso8601Day = "yyyy-MM-dd";
50     
51     private static final String JavaDoc formatRfc822 = "EEE, d MMM yyyy HH:mm:ss Z";
52     
53     
54     /**
55      * Returns a Date set to the last possible millisecond of the day, just
56      * before midnight. If a null day is passed in, a new Date is created.
57      * midnight (00m 00h 00s)
58      */

59     public static Date JavaDoc getEndOfDay(Date JavaDoc day) {
60         return getEndOfDay(day,Calendar.getInstance());
61     }
62     
63     
64     public static Date JavaDoc getEndOfDay(Date JavaDoc day,Calendar JavaDoc cal) {
65         if (day == null) day = new Date JavaDoc();
66         cal.setTime(day);
67         cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
68         cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
69         cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
70         cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
71         return cal.getTime();
72     }
73     
74     
75     /**
76      * Returns a Date set to the first possible millisecond of the month, just
77      * after midnight. If a null day is passed in, a new Date is created.
78      * midnight (00m 00h 00s)
79      */

80     public static Date JavaDoc getStartOfMonth(Date JavaDoc day) {
81         return getStartOfMonth(day, Calendar.getInstance());
82     }
83     
84     
85     public static Date JavaDoc getStartOfMonth(Date JavaDoc day, Calendar JavaDoc cal) {
86         if (day == null) day = new Date JavaDoc();
87         cal.setTime(day);
88         
89         // set time to start of day
90
cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
91         cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
92         cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
93         cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
94         
95         // set time to first day of month
96
cal.set(Calendar.DAY_OF_MONTH, 1);
97         
98         return cal.getTime();
99     }
100     
101     
102     /**
103      * Returns a Date set to the last possible millisecond of the month, just
104      * before midnight. If a null day is passed in, a new Date is created.
105      * midnight (00m 00h 00s)
106      */

107     public static Date JavaDoc getEndOfMonth(Date JavaDoc day) {
108         return getEndOfMonth(day, Calendar.getInstance());
109     }
110     
111     
112     public static Date JavaDoc getEndOfMonth(Date JavaDoc day,Calendar JavaDoc cal) {
113         if (day == null) day = new Date JavaDoc();
114         cal.setTime(day);
115         
116         // set time to end of day
117
cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
118         cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
119         cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
120         cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
121         
122         // set time to first day of month
123
cal.set(Calendar.DAY_OF_MONTH, 1);
124         
125         // add one month
126
cal.add(Calendar.MONTH, 1);
127         
128         // back up one day
129
cal.add(Calendar.DAY_OF_MONTH, -1);
130         
131         return cal.getTime();
132     }
133     
134     
135     /**
136      * Returns a Date set to the first possible millisecond of the day, just
137      * after midnight. If a null day is passed in, a new Date is created.
138      * midnight (00m 00h 00s)
139      */

140     public static Date JavaDoc getStartOfDay(Date JavaDoc day) {
141         return getStartOfDay(day, Calendar.getInstance());
142     }
143     
144     
145     /**
146      * Returns a Date set to the first possible millisecond of the day, just
147      * after midnight. If a null day is passed in, a new Date is created.
148      * midnight (00m 00h 00s)
149      */

150     public static Date JavaDoc getStartOfDay(Date JavaDoc day, Calendar JavaDoc cal) {
151         if (day == null) day = new Date JavaDoc();
152         cal.setTime(day);
153         cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
154         cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
155         cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
156         cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
157         return cal.getTime();
158     }
159     
160     
161     /**
162      * Returns a Date set just to Noon, to the closest possible millisecond
163      * of the day. If a null day is passed in, a new Date is created.
164      * nnoon (00m 12h 00s)
165      */

166     public static Date JavaDoc getNoonOfDay(Date JavaDoc day, Calendar JavaDoc cal) {
167         if (day == null) day = new Date JavaDoc();
168         cal.setTime(day);
169         cal.set(Calendar.HOUR_OF_DAY, 12);
170         cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
171         cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
172         cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
173         return cal.getTime();
174     }
175     
176     
177     /**
178      * Returns a java.sql.Timestamp equal to the current time
179      **/

180     public static java.sql.Timestamp JavaDoc now() {
181         return new java.sql.Timestamp JavaDoc(new java.util.Date JavaDoc().getTime());
182     }
183     
184     
185     /**
186      * Returns a string the represents the passed-in date parsed
187      * according to the passed-in format. Returns an empty string
188      * if the date or the format is null.
189      **/

190     public static String JavaDoc format(Date JavaDoc aDate, SimpleDateFormat JavaDoc aFormat) {
191         if (aDate == null || aFormat == null ) { return ""; }
192         synchronized (aFormat) {
193             return aFormat.format(aDate);
194         }
195     }
196     
197     
198     /**
199      * Returns a Date using the passed-in string and format. Returns null if the string
200      * is null or empty or if the format is null. The string must match the format.
201      **/

202     public static Date JavaDoc parse(String JavaDoc aValue, SimpleDateFormat JavaDoc aFormat) throws ParseException JavaDoc {
203         if (StringUtils.isEmpty(aValue) || aFormat == null) {
204             return null;
205         }
206         synchronized(aFormat) {
207             return aFormat.parse(aValue);
208         }
209     }
210     
211     
212     /**
213      * Returns true if endDate is after startDate or if startDate equals endDate
214      * or if they are the same date. Returns false if either value is null.
215      **/

216     public static boolean isValidDateRange(Date JavaDoc startDate, Date JavaDoc endDate) {
217         return isValidDateRange(startDate, endDate, true);
218     }
219     
220     
221     /**
222      * Returns true if endDate is after startDate or if startDate equals endDate.
223      * Returns false if either value is null. If equalOK, returns true if the
224      * dates are equal.
225      **/

226     public static boolean isValidDateRange(Date JavaDoc startDate, Date JavaDoc endDate, boolean equalOK) {
227         // false if either value is null
228
if (startDate == null || endDate == null) { return false; }
229         
230         if (equalOK) {
231             // true if they are equal
232
if (startDate.equals(endDate)) { return true; }
233         }
234         
235         // true if endDate after startDate
236
if (endDate.after(startDate)) { return true; }
237         
238         return false;
239     }
240     
241     
242     // convenience method returns minimal date format
243
public static SimpleDateFormat JavaDoc defaultDateFormat() {
244         return DateUtil.friendlyDateFormat(true);
245     }
246     
247     
248     // convenience method returns minimal date format
249
public static java.text.SimpleDateFormat JavaDoc minimalDateFormat() {
250         return friendlyDateFormat(true);
251     }
252     
253     
254     // convenience method that returns friendly data format
255
// using full month, day, year digits.
256
public static SimpleDateFormat JavaDoc fullDateFormat() {
257         return friendlyDateFormat(false);
258     }
259     
260     
261     /**
262      * Returns a "friendly" date format.
263      * @param mimimalFormat Should the date format allow single digits.
264      **/

265     public static SimpleDateFormat JavaDoc friendlyDateFormat(boolean minimalFormat) {
266         if (minimalFormat) {
267             return new SimpleDateFormat JavaDoc(formatDefaultDateMinimal);
268         }
269         
270         return new SimpleDateFormat JavaDoc(formatDefaultDate);
271     }
272     
273     
274     // returns full timestamp format
275
public static SimpleDateFormat JavaDoc defaultTimestampFormat() {
276         return new SimpleDateFormat JavaDoc(formatDefaultTimestamp);
277     }
278     
279     
280     // convenience method returns long friendly timestamp format
281
public static SimpleDateFormat JavaDoc friendlyTimestampFormat() {
282         return new SimpleDateFormat JavaDoc(formatFriendlyTimestamp);
283     }
284     
285     
286     // convenience method returns minimal date format
287
public static SimpleDateFormat JavaDoc get8charDateFormat() {
288         return new SimpleDateFormat JavaDoc(format8chars);
289     }
290     
291     
292     // convenience method returns minimal date format
293
public static SimpleDateFormat JavaDoc get6charDateFormat() {
294         return new SimpleDateFormat JavaDoc(format6chars);
295     }
296     
297     
298     // convenience method returns minimal date format
299
public static SimpleDateFormat JavaDoc getIso8601DateFormat() {
300         return new SimpleDateFormat JavaDoc(formatIso8601);
301     }
302     
303     
304     // convenience method returns minimal date format
305
public static SimpleDateFormat JavaDoc getIso8601DayDateFormat() {
306         return new SimpleDateFormat JavaDoc(formatIso8601Day);
307     }
308     
309     
310     // convenience method returns minimal date format
311
public static SimpleDateFormat JavaDoc getRfc822DateFormat() {
312         // http://www.w3.org/Protocols/rfc822/Overview.html#z28
313
// Using Locale.US to fix ROL-725 and ROL-628
314
return new SimpleDateFormat JavaDoc(formatRfc822, Locale.US);
315     }
316     
317     
318     // convenience method
319
public static String JavaDoc defaultDate(Date JavaDoc date) {
320         return format(date, defaultDateFormat());
321     }
322     
323     
324     // convenience method using minimal date format
325
public static String JavaDoc minimalDate(Date JavaDoc date) {
326         return format(date, DateUtil.minimalDateFormat());
327     }
328     
329     
330     public static String JavaDoc fullDate(Date JavaDoc date) {
331         return format(date, DateUtil.fullDateFormat());
332     }
333     
334     
335     /**
336      * Format the date using the "friendly" date format.
337      */

338     public static String JavaDoc friendlyDate(Date JavaDoc date, boolean minimalFormat) {
339         return format(date, friendlyDateFormat(minimalFormat));
340     }
341     
342     
343     // convenience method
344
public static String JavaDoc friendlyDate(Date JavaDoc date) {
345         return format(date, friendlyDateFormat(true));
346     }
347     
348     
349     // convenience method
350
public static String JavaDoc defaultTimestamp(Date JavaDoc date) {
351         return format(date, defaultTimestampFormat());
352     }
353     
354     
355     // convenience method returns long friendly formatted timestamp
356
public static String JavaDoc friendlyTimestamp(Date JavaDoc date) {
357         return format(date, friendlyTimestampFormat());
358     }
359     
360     
361     // convenience method returns 8 char day stamp YYYYMMDD
362
public static String JavaDoc format8chars(Date JavaDoc date) {
363         return format(date, get8charDateFormat());
364     }
365     
366     
367     // convenience method returns 6 char month stamp YYYYMM
368
public static String JavaDoc format6chars(Date JavaDoc date) {
369         return format(date, get6charDateFormat());
370     }
371     
372     
373     // convenience method returns long friendly formatted timestamp
374
public static String JavaDoc formatIso8601Day(Date JavaDoc date) {
375         return format(date, getIso8601DayDateFormat());
376     }
377     
378     
379     public static String JavaDoc formatRfc822(Date JavaDoc date) {
380         return format(date, getRfc822DateFormat());
381     }
382     
383     
384     // This is a hack, but it seems to work
385
public static String JavaDoc formatIso8601(Date JavaDoc date) {
386         if (date == null) return "";
387         
388         // Add a colon 2 chars before the end of the string
389
// to make it a valid ISO-8601 date.
390

391         String JavaDoc str = format(date, getIso8601DateFormat());
392         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
393         sb.append( str.substring(0,str.length()-2) );
394         sb.append( ":" );
395         sb.append( str.substring(str.length()-2) );
396         return sb.toString();
397     }
398     
399     
400     public static Date JavaDoc parseIso8601(String JavaDoc value) throws Exception JavaDoc {
401         return ISO8601DateParser.parse(value);
402     }
403     
404     
405     /**
406      * Parse data as either 6-char or 8-char format.
407      */

408     public static Date JavaDoc parseWeblogURLDateString(String JavaDoc dateString, TimeZone JavaDoc tz, Locale JavaDoc locale) {
409         
410         Date JavaDoc ret = new Date JavaDoc();
411         SimpleDateFormat JavaDoc char8DateFormat = DateUtil.get8charDateFormat();
412         SimpleDateFormat JavaDoc char6DateFormat = DateUtil.get6charDateFormat();
413         
414         if (dateString != null
415                 && dateString.length()==8
416                 && StringUtils.isNumeric(dateString) ) {
417             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
418             ret = char8DateFormat.parse(dateString, pos);
419             
420             // make sure the requested date is not in the future
421
Date JavaDoc today = null;
422             Calendar JavaDoc todayCal = Calendar.getInstance();
423             todayCal = Calendar.getInstance(tz, locale);
424             todayCal.setTime(new Date JavaDoc());
425             today = todayCal.getTime();
426             if(ret.after(today)) {
427                 ret = today;
428             }
429             
430         } else if(dateString != null
431                 && dateString.length()==6
432                 && StringUtils.isNumeric(dateString)) {
433             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
434             ret = char6DateFormat.parse(dateString, pos);
435             
436             // make sure the requested date is not in the future
437
Calendar JavaDoc todayCal = Calendar.getInstance();
438             todayCal = Calendar.getInstance(tz, locale);
439             todayCal.setTime(new Date JavaDoc());
440             Date JavaDoc today = todayCal.getTime();
441             if(ret.after(today)) {
442                 ret = today;
443             }
444         }
445         
446         return ret;
447     }
448     
449 }
450
Popular Tags