KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > common > util > DateUtil


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.common.util;
17
18 import sun.text.resources.LocaleData;
19
20 import java.text.DateFormat JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27
28 /**
29  * <p>Date Utility Class
30  * This is used to convert Strings to Dates and Timestamps and vice versa.
31  * </p>
32  * <p><a HREF="DateUtil.java.htm"><i>View Source</i></a>
33  * </p>
34  *
35  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
36  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
37  * @version $Revision: 1.8 $ $Date: 2005/10/05 18:52:29 $
38  */

39 public class DateUtil {
40     //~ Static fields/initializers =============================================
41

42     protected static String JavaDoc datePattern = "MM/dd/yyyy";
43     protected static HashMap JavaDoc patterns = new HashMap JavaDoc();
44
45
46     //~ Methods ================================================================
47

48     /**
49      * Formats given date according to specified locale and date style
50      *
51      * @param date Date to convert
52      * @param locale Locale to use for formatting date
53      * @param dateStyle Date style
54      * @return String representation of date according to given locale and date style
55      * @see java.text.DateFormat
56      */

57     public static final String JavaDoc formatDate(Date JavaDoc date, Locale JavaDoc locale, int dateStyle) {
58         DateFormat JavaDoc formatter = DateFormat.getDateInstance(dateStyle, locale);
59         return formatter.format(date);
60     }
61
62     /**
63      * Formats given date according to specified locale and <code>DateFormat.MEDIUM</code> style
64      *
65      * @param date Date to convert
66      * @param locale Locale to use for formatting date
67      * @return String representation of date according to given locale and <code>DateFormat.MEDIUM</code> style
68      * @see java.text.DateFormat
69      * @see java.text.DateFormat#MEDIUM
70      */

71     public static final String JavaDoc formatDate(Date JavaDoc date, Locale JavaDoc locale) {
72         return formatDate(date, locale, DateFormat.MEDIUM);
73     }
74
75     /**
76      * Parses given string according to specified locale and date style
77      *
78      * @param source Source string to parse date from
79      * @param locale Locale to use for parsing date
80      * @param dateStyle Date style
81      * @return Date object corresponding to representation given in source string
82      * @throws ParseException if given string could not be properly parsed according to given locale and style
83      * @see java.text.DateFormat
84      */

85     public static final Date JavaDoc parseDate(String JavaDoc source, Locale JavaDoc locale, int dateStyle) throws ParseException JavaDoc {
86         DateFormat JavaDoc formatter = DateFormat.getDateInstance(dateStyle, locale);
87         return formatter.parse(source);
88     }
89
90     /**
91      * Parses given string according to specified locale and <code>DateFormat.MEDIUM</code> style
92      *
93      * @param source Source string to parse date from
94      * @param locale Locale to use for parsing date
95      * @return Date object corresponding to representation given in source string
96      * @throws ParseException if given string could not be properly parsed according to given locale and <code>DateFormat.MEDIUM</code> style
97      * @see java.text.DateFormat
98      * @see java.text.DateFormat#MEDIUM
99      */

100     public static final Date JavaDoc parseDate(String JavaDoc source, Locale JavaDoc locale) throws ParseException JavaDoc {
101         return parseDate(source, locale, DateFormat.MEDIUM);
102     }
103
104
105     /**
106      * Formats given time according to specified locale and time style
107      *
108      * @param time Time to convert
109      * @param locale Locale to use for formatting time
110      * @param timeStyle Time style
111      * @return String representation of time according to given locale and time style
112      * @see java.text.DateFormat
113      */

114     public static final String JavaDoc formatTime(Date JavaDoc time, Locale JavaDoc locale, int timeStyle) {
115         DateFormat JavaDoc formatter = DateFormat.getTimeInstance(timeStyle, locale);
116         return formatter.format(time);
117     }
118
119     /**
120      * Formats given time according to specified locale and <code>DateFormat.MEDIUM</code> style
121      *
122      * @param time Time to convert
123      * @param locale Locale to use for formatting time
124      * @return String representation of time according to given locale and <code>DateFormat.MEDIUM</code> style
125      * @see java.text.DateFormat
126      * @see java.text.DateFormat#MEDIUM
127      */

128     public static final String JavaDoc formatTime(Date JavaDoc time, Locale JavaDoc locale) {
129         return formatTime(time, locale, DateFormat.MEDIUM);
130     }
131
132     /**
133      * Parses given string according to specified locale and time style
134      *
135      * @param source Source string to parse time from
136      * @param locale Locale to use for parsing time
137      * @param timeStyle Time style
138      * @return Time object corresponding to representation given in source string
139      * @throws ParseException if given string could not be properly parsed according to given locale and style
140      * @see java.text.DateFormat
141      */

142     public static final Date JavaDoc parseTime(String JavaDoc source, Locale JavaDoc locale, int timeStyle) throws ParseException JavaDoc {
143         DateFormat JavaDoc formatter = DateFormat.getTimeInstance(timeStyle, locale);
144         return formatter.parse(source);
145     }
146
147     /**
148      * Parses given string according to specified locale and <code>DateFormat.MEDIUM</code> style
149      *
150      * @param source Source string to parse time from
151      * @param locale Locale to use for parsing time
152      * @return Time object corresponding to representation given in source string
153      * @throws ParseException if given string could not be properly parsed according to given locale and <code>DateFormat.MEDIUM</code> style
154      * @see java.text.DateFormat
155      * @see java.text.DateFormat#MEDIUM
156      */

157     public static final Date JavaDoc parseTime(String JavaDoc source, Locale JavaDoc locale) throws ParseException JavaDoc {
158         return parseTime(source, locale, DateFormat.MEDIUM);
159     }
160
161     /**
162      * Formats given date and time according to specified locale and date style
163      *
164      * @param date Date object to convert
165      * @param locale Locale to use for formatting date and time
166      * @param dateStyle Date style
167      * @param timeStyle Time style
168      * @return String representation of date and time according to given locale and date style
169      * @see java.text.DateFormat
170      */

171     public static final String JavaDoc formatDateTime(Date JavaDoc date, Locale JavaDoc locale, int dateStyle, int timeStyle) {
172         DateFormat JavaDoc formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
173         return formatter.format(date);
174     }
175
176     /**
177      * Formats given date and time according to specified locale and <code>DateFormat.MEDIUM</code> style
178      *
179      * @param date Date object to convert
180      * @param locale Locale to use for formatting date and time
181      * @return String representation of date and time according to given locale and <code>DateFormat.MEDIUM</code> style
182      * @see java.text.DateFormat
183      * @see java.text.DateFormat#MEDIUM
184      */

185     public static final String JavaDoc formatDateTime(Date JavaDoc date, Locale JavaDoc locale) {
186         return formatDateTime(date, locale, DateFormat.MEDIUM, DateFormat.MEDIUM);
187     }
188
189     /**
190      * Parses given string according to specified locale and date and time styles
191      *
192      * @param source Source string to parse date and time from
193      * @param locale Locale to use for parsing date and time
194      * @param dateStyle Date style
195      * @param timeStyle Time style
196      * @return Date object corresponding to representation given in source string
197      * @throws ParseException if given string could not be properly parsed according to given locale and style
198      * @see java.text.DateFormat
199      */

200     public static final Date JavaDoc parseDateTime(String JavaDoc source, Locale JavaDoc locale, int dateStyle, int timeStyle) throws ParseException JavaDoc {
201         DateFormat JavaDoc formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
202         return formatter.parse(source);
203     }
204
205     /**
206      * Parses given string according to specified locale and <code>DateFormat.MEDIUM</code> style
207      *
208      * @param source Source string to parse date and time from
209      * @param locale Locale to use for parsing date and time
210      * @return Date object corresponding to representation given in source string
211      * @throws ParseException if given string could not be properly parsed according to given locale and <code>DateFormat.MEDIUM</code> style
212      * @see java.text.DateFormat
213      * @see java.text.DateFormat#MEDIUM
214      */

215     public static final Date JavaDoc parseDateTime(String JavaDoc source, Locale JavaDoc locale) throws ParseException JavaDoc {
216         return parseDateTime(source, locale, DateFormat.MEDIUM, DateFormat.MEDIUM);
217     }
218
219     /**
220      * Returns default datePattern (MM/dd/yyyy)
221      *
222      * @return a string representing the date pattern on the UI
223      */

224     public static String JavaDoc getDatePattern() {
225         return datePattern;
226     }
227
228     /**
229      * Gets date pattern without time symbols according to given locale and date style
230      *
231      * @param locale Locale to searh pattern for
232      * @param dateStyle Date style to search pattern by
233      * @return Date pattern without time symbols
234      */

235     public static String JavaDoc getDatePattern(Locale JavaDoc locale, int dateStyle) {
236         String JavaDoc[] dateTimePatterns = (String JavaDoc[])patterns.get(locale);
237         if (dateTimePatterns == null) {
238             ResourceBundle JavaDoc r = LocaleData.getLocaleElements(locale);
239             dateTimePatterns = r.getStringArray("DateTimePatterns");
240             patterns.put(locale, dateTimePatterns);
241         }
242         return dateTimePatterns[dateStyle + 4];
243     }
244
245     /**
246      * Gets date pattern without time symbols according to given locale in MEDIUM style
247      *
248      * @param locale Locale to searh pattern for
249      * @return Date pattern without time symbols in medium format
250      * @see java.text.DateFormat#MEDIUM
251      */

252     public static String JavaDoc getDatePattern(Locale JavaDoc locale) {
253         return getDatePattern(locale, DateFormat.MEDIUM);
254     }
255
256
257 }
258
Popular Tags