View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.log4j.lf5.util;
18  
19  import java.text.DateFormat;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.Locale;
24  import java.util.TimeZone;
25  
26  /**
27   * Date format manager.
28   * Utility class to help manage consistent date formatting and parsing.
29   * It may be advantageous to have multiple DateFormatManagers per
30   * application.  For example, one for handling the output (formatting) of
31   * dates, and another one for handling the input (parsing) of dates.
32   *
33   * @author Robert Shaw
34   * @author Michael J. Sikorsky
35   */
36  
37  // Contributed by ThoughtWorks Inc.
38  public class DateFormatManager {
39    //--------------------------------------------------------------------------
40    //   Constants:
41    //--------------------------------------------------------------------------
42  
43    //--------------------------------------------------------------------------
44    //   Protected Variables:
45    //--------------------------------------------------------------------------
46  
47    //--------------------------------------------------------------------------
48    //   Private Variables:
49    //--------------------------------------------------------------------------
50    private TimeZone _timeZone = null;
51    private Locale _locale = null;
52  
53    private String _pattern = null;
54    private DateFormat _dateFormat = null;
55  
56    //--------------------------------------------------------------------------
57    //   Constructors:
58    //--------------------------------------------------------------------------
59    public DateFormatManager() {
60      super();
61      configure();
62    }
63  
64    public DateFormatManager(TimeZone timeZone) {
65      super();
66  
67      _timeZone = timeZone;
68      configure();
69    }
70  
71    public DateFormatManager(Locale locale) {
72      super();
73  
74      _locale = locale;
75      configure();
76    }
77  
78    public DateFormatManager(String pattern) {
79      super();
80  
81      _pattern = pattern;
82      configure();
83    }
84  
85    public DateFormatManager(TimeZone timeZone, Locale locale) {
86      super();
87  
88      _timeZone = timeZone;
89      _locale = locale;
90      configure();
91    }
92  
93    public DateFormatManager(TimeZone timeZone, String pattern) {
94      super();
95  
96      _timeZone = timeZone;
97      _pattern = pattern;
98      configure();
99    }
100 
101   public DateFormatManager(Locale locale, String pattern) {
102     super();
103 
104     _locale = locale;
105     _pattern = pattern;
106     configure();
107   }
108 
109   public DateFormatManager(TimeZone timeZone, Locale locale, String pattern) {
110     super();
111 
112     _timeZone = timeZone;
113     _locale = locale;
114     _pattern = pattern;
115     configure();
116   }
117 
118   //--------------------------------------------------------------------------
119   //   Public Methods:
120   //--------------------------------------------------------------------------
121 
122   public synchronized TimeZone getTimeZone() {
123     if (_timeZone == null) {
124       return TimeZone.getDefault();
125     } else {
126       return _timeZone;
127     }
128   }
129 
130   public synchronized void setTimeZone(TimeZone timeZone) {
131     _timeZone = timeZone;
132     configure();
133   }
134 
135   public synchronized Locale getLocale() {
136     if (_locale == null) {
137       return Locale.getDefault();
138     } else {
139       return _locale;
140     }
141   }
142 
143   public synchronized void setLocale(Locale locale) {
144     _locale = locale;
145     configure();
146   }
147 
148   public synchronized String getPattern() {
149     return _pattern;
150   }
151 
152   /**
153    * Set the pattern. i.e. "EEEEE, MMMMM d, yyyy hh:mm aaa"
154    */
155   public synchronized void setPattern(String pattern) {
156     _pattern = pattern;
157     configure();
158   }
159 
160 
161   /**
162    * This method has been deprecated in favour of getPattern().
163    * @deprecated Use getPattern().
164    */
165   public synchronized String getOutputFormat() {
166     return _pattern;
167   }
168 
169   /**
170    * This method has been deprecated in favour of setPattern().
171    * @deprecated Use setPattern().
172    */
173   public synchronized void setOutputFormat(String pattern) {
174     _pattern = pattern;
175     configure();
176   }
177 
178   public synchronized DateFormat getDateFormatInstance() {
179     return _dateFormat;
180   }
181 
182   public synchronized void setDateFormatInstance(DateFormat dateFormat) {
183     _dateFormat = dateFormat;
184     // No reconfiguration necessary!
185   }
186 
187   public String format(Date date) {
188     return getDateFormatInstance().format(date);
189   }
190 
191   public String format(Date date, String pattern) {
192     DateFormat formatter = null;
193     formatter = getDateFormatInstance();
194     if (formatter instanceof SimpleDateFormat) {
195       formatter = (SimpleDateFormat) (formatter.clone());
196       ((SimpleDateFormat) formatter).applyPattern(pattern);
197     }
198     return formatter.format(date);
199   }
200 
201   /**
202    * @throws java.text.ParseException
203    */
204   public Date parse(String date) throws ParseException {
205     return getDateFormatInstance().parse(date);
206   }
207 
208   /**
209    * @throws java.text.ParseException
210    */
211   public Date parse(String date, String pattern) throws ParseException {
212     DateFormat formatter = null;
213     formatter = getDateFormatInstance();
214     if (formatter instanceof SimpleDateFormat) {
215       formatter = (SimpleDateFormat) (formatter.clone());
216       ((SimpleDateFormat) formatter).applyPattern(pattern);
217     }
218     return formatter.parse(date);
219   }
220 
221   //--------------------------------------------------------------------------
222   //   Protected Methods:
223   //--------------------------------------------------------------------------
224 
225   //--------------------------------------------------------------------------
226   //   Private Methods:
227   //--------------------------------------------------------------------------
228   private synchronized void configure() {
229     _dateFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.FULL,
230         DateFormat.FULL,
231         getLocale());
232     _dateFormat.setTimeZone(getTimeZone());
233 
234     if (_pattern != null) {
235       ((SimpleDateFormat) _dateFormat).applyPattern(_pattern);
236     }
237   }
238 
239   //--------------------------------------------------------------------------
240   //   Nested Top-Level Classes or Interfaces:
241   //--------------------------------------------------------------------------
242 
243 }