KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > util > DateFormatManager


1 /*
2  * Copyright 1999-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.log4j.lf5.util;
17
18 import java.text.DateFormat JavaDoc;
19 import java.text.ParseException JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.TimeZone JavaDoc;
24
25 /**
26  * Date format manager.
27  * Utility class to help manage consistent date formatting and parsing.
28  * It may be advantageous to have multiple DateFormatManagers per
29  * application. For example, one for handling the output (formatting) of
30  * dates, and another one for handling the input (parsing) of dates.
31  *
32  * @author Robert Shaw
33  * @author Michael J. Sikorsky
34  */

35
36 // Contributed by ThoughtWorks Inc.
37
public class DateFormatManager {
38   //--------------------------------------------------------------------------
39
// Constants:
40
//--------------------------------------------------------------------------
41

42   //--------------------------------------------------------------------------
43
// Protected Variables:
44
//--------------------------------------------------------------------------
45

46   //--------------------------------------------------------------------------
47
// Private Variables:
48
//--------------------------------------------------------------------------
49
private TimeZone JavaDoc _timeZone = null;
50   private Locale JavaDoc _locale = null;
51
52   private String JavaDoc _pattern = null;
53   private DateFormat JavaDoc _dateFormat = null;
54
55   //--------------------------------------------------------------------------
56
// Constructors:
57
//--------------------------------------------------------------------------
58
public DateFormatManager() {
59     super();
60     configure();
61   }
62
63   public DateFormatManager(TimeZone JavaDoc timeZone) {
64     super();
65
66     _timeZone = timeZone;
67     configure();
68   }
69
70   public DateFormatManager(Locale JavaDoc locale) {
71     super();
72
73     _locale = locale;
74     configure();
75   }
76
77   public DateFormatManager(String JavaDoc pattern) {
78     super();
79
80     _pattern = pattern;
81     configure();
82   }
83
84   public DateFormatManager(TimeZone JavaDoc timeZone, Locale JavaDoc locale) {
85     super();
86
87     _timeZone = timeZone;
88     _locale = locale;
89     configure();
90   }
91
92   public DateFormatManager(TimeZone JavaDoc timeZone, String JavaDoc pattern) {
93     super();
94
95     _timeZone = timeZone;
96     _pattern = pattern;
97     configure();
98   }
99
100   public DateFormatManager(Locale JavaDoc locale, String JavaDoc pattern) {
101     super();
102
103     _locale = locale;
104     _pattern = pattern;
105     configure();
106   }
107
108   public DateFormatManager(TimeZone JavaDoc timeZone, Locale JavaDoc locale, String JavaDoc pattern) {
109     super();
110
111     _timeZone = timeZone;
112     _locale = locale;
113     _pattern = pattern;
114     configure();
115   }
116
117   //--------------------------------------------------------------------------
118
// Public Methods:
119
//--------------------------------------------------------------------------
120

121   public synchronized TimeZone JavaDoc getTimeZone() {
122     if (_timeZone == null) {
123       return TimeZone.getDefault();
124     } else {
125       return _timeZone;
126     }
127   }
128
129   public synchronized void setTimeZone(TimeZone JavaDoc timeZone) {
130     timeZone = timeZone;
131     configure();
132   }
133
134   public synchronized Locale JavaDoc getLocale() {
135     if (_locale == null) {
136       return Locale.getDefault();
137     } else {
138       return _locale;
139     }
140   }
141
142   public synchronized void setLocale(Locale JavaDoc locale) {
143     _locale = locale;
144     configure();
145   }
146
147   public synchronized String JavaDoc getPattern() {
148     return _pattern;
149   }
150
151   /**
152    * Set the pattern. i.e. "EEEEE, MMMMM d, yyyy hh:mm aaa"
153    */

154   public synchronized void setPattern(String JavaDoc pattern) {
155     _pattern = pattern;
156     configure();
157   }
158
159
160   /**
161    * This method has been deprecated in favour of getPattern().
162    * @deprecated Use getPattern().
163    */

164   public synchronized String JavaDoc getOutputFormat() {
165     return _pattern;
166   }
167
168   /**
169    * This method has been deprecated in favour of setPattern().
170    * @deprecated Use setPattern().
171    */

172   public synchronized void setOutputFormat(String JavaDoc pattern) {
173     _pattern = pattern;
174     configure();
175   }
176
177   public synchronized DateFormat JavaDoc getDateFormatInstance() {
178     return _dateFormat;
179   }
180
181   public synchronized void setDateFormatInstance(DateFormat JavaDoc dateFormat) {
182     _dateFormat = dateFormat;
183     // No reconfiguration necessary!
184
}
185
186   public String JavaDoc format(Date JavaDoc date) {
187     return getDateFormatInstance().format(date);
188   }
189
190   public String JavaDoc format(Date JavaDoc date, String JavaDoc pattern) {
191     DateFormat JavaDoc formatter = null;
192     formatter = getDateFormatInstance();
193     if (formatter instanceof SimpleDateFormat JavaDoc) {
194       formatter = (SimpleDateFormat JavaDoc) (formatter.clone());
195       ((SimpleDateFormat JavaDoc) formatter).applyPattern(pattern);
196     }
197     return formatter.format(date);
198   }
199
200   /**
201    * @throws java.text.ParseException
202    */

203   public Date JavaDoc parse(String JavaDoc date) throws ParseException JavaDoc {
204     return getDateFormatInstance().parse(date);
205   }
206
207   /**
208    * @throws java.text.ParseException
209    */

210   public Date JavaDoc parse(String JavaDoc date, String JavaDoc pattern) throws ParseException JavaDoc {
211     DateFormat JavaDoc formatter = null;
212     formatter = getDateFormatInstance();
213     if (formatter instanceof SimpleDateFormat JavaDoc) {
214       formatter = (SimpleDateFormat JavaDoc) (formatter.clone());
215       ((SimpleDateFormat JavaDoc) formatter).applyPattern(pattern);
216     }
217     return formatter.parse(date);
218   }
219
220   //--------------------------------------------------------------------------
221
// Protected Methods:
222
//--------------------------------------------------------------------------
223

224   //--------------------------------------------------------------------------
225
// Private Methods:
226
//--------------------------------------------------------------------------
227
private synchronized void configure() {
228     _dateFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.FULL,
229         DateFormat.FULL,
230         getLocale());
231     _dateFormat.setTimeZone(getTimeZone());
232
233     if (_pattern != null) {
234       ((SimpleDateFormat JavaDoc) _dateFormat).applyPattern(_pattern);
235     }
236   }
237
238   //--------------------------------------------------------------------------
239
// Nested Top-Level Classes or Interfaces:
240
//--------------------------------------------------------------------------
241

242 }
243
Popular Tags