KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > DateUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import info.magnolia.context.MgnlContext;
16
17 import java.text.DateFormat JavaDoc;
18 import java.text.ParseException JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.TimeZone JavaDoc;
23
24 import org.apache.commons.lang.time.DateFormatUtils;
25 import org.apache.commons.lang.time.FastDateFormat;
26
27
28 /**
29  * @author Sameer Charles
30  * @version $Revision: 7286 $ ($Author: philipp $)
31  */

32 public class DateUtil {
33
34     /**
35      * Default date format.
36      */

37     public static final String JavaDoc FORMAT_DEFAULTPATTERN = "yyyy-MM-dd'T'HH:mm:ss.SZ"; //$NON-NLS-1$
38

39     public static final String JavaDoc YYYY_MM_DD = "yyyy-MM-dd";
40
41     public static final String JavaDoc YYYY_MM_DD_T_HH_MM_SS = "yyyy-MM-dd' 'HH:mm:ss";
42
43     public static final TimeZone JavaDoc UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");
44
45     public String JavaDoc getFormattedDate(Date JavaDoc date) {
46         return this.getFormattedDate(date, FORMAT_DEFAULTPATTERN);
47     }
48
49     public String JavaDoc getFormattedDate(Date JavaDoc date, String JavaDoc formatPattern) {
50         if (formatPattern == null) {
51             formatPattern = FORMAT_DEFAULTPATTERN;
52         }
53         return DateFormatUtils.formatUTC(date, formatPattern);
54     }
55
56     /**
57      * Uses the current locale (user) to format the date and time
58      * @param val Date or Calendar
59      * @return the String
60      */

61     public static String JavaDoc formatDateTime(Object JavaDoc val) {
62         FastDateFormat format = FastDateFormat.getDateTimeInstance(
63             FastDateFormat.SHORT,
64             FastDateFormat.SHORT,
65             MgnlContext.getLocale());
66         return format.format(val);
67     }
68     
69     /**
70      * Uses the current locale (user) to format the date
71      * @param val Date or Calendar
72      * @return the String
73      */

74     public static String JavaDoc formatDate(Object JavaDoc val) {
75         FastDateFormat format = FastDateFormat.getDateInstance(
76             FastDateFormat.SHORT,
77             MgnlContext.getLocale());
78         return format.format(val);
79     }
80
81     /**
82      * Uses the current locale (user) to parse the date and time
83      * @param val Date or Calendar
84      * @return the String
85      * @throws ParseException
86      */

87     public static Date JavaDoc parseDateTime(String JavaDoc dateStr) throws ParseException JavaDoc {
88         DateFormat JavaDoc format = SimpleDateFormat.getDateTimeInstance(
89             FastDateFormat.SHORT,
90             FastDateFormat.SHORT,
91             MgnlContext.getLocale());
92         return (Date JavaDoc)format.parseObject(dateStr);
93     }
94     
95     /**
96      * Uses the current locale (user) to parse the date
97      * @param val Date or Calendar
98      * @return the String
99      * @throws ParseException
100      */

101     public static Date JavaDoc parseDate(String JavaDoc dateStr) throws ParseException JavaDoc {
102         DateFormat JavaDoc format = SimpleDateFormat.getDateInstance(
103             FastDateFormat.SHORT,
104             MgnlContext.getLocale());
105         return format.parse(dateStr);
106     }
107     
108     /**
109      * Get the equivalent UTC calendar to a local calendar
110      */

111     public static Calendar JavaDoc getLocalCalendarFromUTC(Calendar JavaDoc utc) {
112         Date JavaDoc valueDate = utc.getTime();
113         Calendar JavaDoc c = Calendar.getInstance(); // this has the default timezone for the server
114
c.setTime(valueDate);
115         return c;
116     }
117
118     /**
119      * Convert a string date from a dialog date to a UTC calendar ready to be stored in the repository
120      */

121     public static Calendar JavaDoc getUTCCalendarFromDialogString(String JavaDoc dateString) throws ParseException JavaDoc {
122         SimpleDateFormat JavaDoc sdf = (dateString.length() > YYYY_MM_DD.length())
123             ? new SimpleDateFormat JavaDoc(YYYY_MM_DD_T_HH_MM_SS)
124             : new SimpleDateFormat JavaDoc(YYYY_MM_DD);
125         return getUTCCalendarFromLocalDate(sdf.parse(dateString));
126     }
127
128     /**
129      * Convert a local date time to a UTC calendar
130      */

131     public static Calendar JavaDoc getUTCCalendarFromLocalDate(Date JavaDoc date) {
132         Calendar JavaDoc instance = getCurrentUTCCalendar();
133         instance.setTimeInMillis(date.getTime());
134         return instance;
135     }
136
137     /**
138      * Get UTC Calendar for current time
139      */

140     public static Calendar JavaDoc getCurrentUTCCalendar() {
141         return Calendar.getInstance(UTC_TIME_ZONE);
142     }
143 }
144
Popular Tags