KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > CmsDateUtil


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsDateUtil.java,v $
3  * Date : $Date: 2006/03/27 14:52:41 $
4  * Version: $Revision: 1.17 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import org.opencms.i18n.CmsLocaleManager;
35
36 import java.text.DateFormat JavaDoc;
37 import java.text.ParseException JavaDoc;
38 import java.text.SimpleDateFormat JavaDoc;
39 import java.util.Calendar JavaDoc;
40 import java.util.Date JavaDoc;
41 import java.util.GregorianCalendar JavaDoc;
42 import java.util.Locale JavaDoc;
43 import java.util.TimeZone JavaDoc;
44
45 /**
46  *
47  * Utilities to get and set formated dates in OpenCms.<p>
48  *
49  * @author Michael Emmerich
50  *
51  * @version $Revision: 1.17 $
52  *
53  * @since 6.0.0
54  */

55 public final class CmsDateUtil {
56
57     /** The "GMT" time zone, used when formatting http headers. */
58     protected static final TimeZone JavaDoc GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
59
60     /** The default format to use when formatting http headers. */
61     protected static final DateFormat JavaDoc HEADER_DEFAULT = new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
62
63     /** The default format to use when formatting old cookies. */
64     protected static final DateFormat JavaDoc OLD_COOKIE = new SimpleDateFormat JavaDoc("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
65
66     /**
67      * Hides the public constructor.<p>
68      */

69     private CmsDateUtil() {
70
71         // noop
72
}
73
74     /**
75      * Returns a formated date String from a Date value,
76      * the formatting based on the provided options.<p>
77      *
78      * @param date the Date object to format as String
79      * @param format the format to use, see {@link DateFormat} for possible values
80      * @param locale the locale to use
81      * @return the formatted date
82      */

83     public static String JavaDoc getDate(Date JavaDoc date, int format, Locale JavaDoc locale) {
84
85         DateFormat JavaDoc df = DateFormat.getDateInstance(format, locale);
86         return df.format(date);
87     }
88
89     /**
90      * Returns a formated date String form a timestamp value,
91      * the formatting based on the OpenCms system default locale
92      * and the {@link DateFormat#SHORT} date format.<p>
93      *
94      * @param time the time value to format as date
95      * @return the formatted date
96      */

97     public static String JavaDoc getDateShort(long time) {
98
99         return getDate(new Date JavaDoc(time), DateFormat.SHORT, CmsLocaleManager.getDefaultLocale());
100     }
101
102     /**
103      * Returns a formated date and time String from a Date value,
104      * the formatting based on the provided options.<p>
105      *
106      * @param date the Date object to format as String
107      * @param format the format to use, see {@link DateFormat} for possible values
108      * @param locale the locale to use
109      * @return the formatted date
110      */

111     public static String JavaDoc getDateTime(Date JavaDoc date, int format, Locale JavaDoc locale) {
112
113         DateFormat JavaDoc df = DateFormat.getDateInstance(format, locale);
114         DateFormat JavaDoc tf = DateFormat.getTimeInstance(format, locale);
115         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
116         buf.append(df.format(date));
117         buf.append(" ");
118         buf.append(tf.format(date));
119         return buf.toString();
120     }
121
122     /**
123      * Returns a formated date and time String form a timestamp value,
124      * the formatting based on the OpenCms system default locale
125      * and the {@link DateFormat#SHORT} date format.<p>
126      *
127      * @param time the time value to format as date
128      * @return the formatted date
129      */

130     public static String JavaDoc getDateTimeShort(long time) {
131
132         return getDateTime(new Date JavaDoc(time), DateFormat.SHORT, CmsLocaleManager.getDefaultLocale());
133     }
134
135     /**
136      * Returns the number of days passed since a specific date.<p>
137      *
138      * @param dateLastModified the date to compute the passed days from
139      *
140      * @return the number of days passed since a specific date
141      */

142     public static int getDaysPassedSince(Date JavaDoc dateLastModified) {
143
144         GregorianCalendar JavaDoc now = new GregorianCalendar JavaDoc();
145         GregorianCalendar JavaDoc lastModified = (GregorianCalendar JavaDoc)now.clone();
146         lastModified.setTimeInMillis(dateLastModified.getTime());
147         return now.get(Calendar.DAY_OF_YEAR)
148             - lastModified.get(Calendar.DAY_OF_YEAR)
149             + (now.get(Calendar.YEAR) - lastModified.get(Calendar.YEAR))
150             * 365;
151     }
152
153     /**
154      * Returns a formated date and time String form a timestamp value based on the
155      * HTTP-Header date format.<p>
156      *
157      * @param time the time value to format as date
158      * @return the formatted date
159      */

160     public static String JavaDoc getHeaderDate(long time) {
161
162         if (HEADER_DEFAULT.getTimeZone() != GMT_TIMEZONE) {
163             // ensure GMT is used as time zone for the header generation
164
HEADER_DEFAULT.setTimeZone(GMT_TIMEZONE);
165         }
166
167         return HEADER_DEFAULT.format(new Date JavaDoc(time));
168     }
169
170     /**
171      * Returns a formated date and time String form a timestamp value based on the
172      * (old) Netscape cookie date format.<p>
173      *
174      * @param time the time value to format as date
175      * @return the formatted date
176      */

177     public static String JavaDoc getOldCookieDate(long time) {
178
179         if (OLD_COOKIE.getTimeZone() != GMT_TIMEZONE) {
180             // ensure GMT is used as time zone for the header generation
181
OLD_COOKIE.setTimeZone(GMT_TIMEZONE);
182         }
183
184         return OLD_COOKIE.format(new Date JavaDoc(time));
185     }
186
187     /**
188      * Parses a formated date and time string in HTTP-Header date format and returns the
189      * time value.<p>
190      *
191      * @param timestamp the timestamp in HTTP-Header date format
192      * @return time value as long
193      * @throws ParseException if parsing fails
194      */

195     public static long parseHeaderDate(String JavaDoc timestamp) throws ParseException JavaDoc {
196
197         return HEADER_DEFAULT.parse(timestamp).getTime();
198     }
199 }
Popular Tags