001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.log4j.lf5.util;
018
019import java.text.DateFormat;
020import java.text.ParseException;
021import java.text.SimpleDateFormat;
022import java.util.Date;
023import java.util.Locale;
024import java.util.TimeZone;
025
026/**
027 * Date format manager.
028 * Utility class to help manage consistent date formatting and parsing.
029 * It may be advantageous to have multiple DateFormatManagers per
030 * application.  For example, one for handling the output (formatting) of
031 * dates, and another one for handling the input (parsing) of dates.
032 *
033 * @author Robert Shaw
034 * @author Michael J. Sikorsky
035 */
036
037// Contributed by ThoughtWorks Inc.
038public class DateFormatManager {
039  //--------------------------------------------------------------------------
040  //   Constants:
041  //--------------------------------------------------------------------------
042
043  //--------------------------------------------------------------------------
044  //   Protected Variables:
045  //--------------------------------------------------------------------------
046
047  //--------------------------------------------------------------------------
048  //   Private Variables:
049  //--------------------------------------------------------------------------
050  private TimeZone _timeZone = null;
051  private Locale _locale = null;
052
053  private String _pattern = null;
054  private DateFormat _dateFormat = null;
055
056  //--------------------------------------------------------------------------
057  //   Constructors:
058  //--------------------------------------------------------------------------
059  public DateFormatManager() {
060    super();
061    configure();
062  }
063
064  public DateFormatManager(TimeZone timeZone) {
065    super();
066
067    _timeZone = timeZone;
068    configure();
069  }
070
071  public DateFormatManager(Locale locale) {
072    super();
073
074    _locale = locale;
075    configure();
076  }
077
078  public DateFormatManager(String pattern) {
079    super();
080
081    _pattern = pattern;
082    configure();
083  }
084
085  public DateFormatManager(TimeZone timeZone, Locale locale) {
086    super();
087
088    _timeZone = timeZone;
089    _locale = locale;
090    configure();
091  }
092
093  public DateFormatManager(TimeZone timeZone, String pattern) {
094    super();
095
096    _timeZone = timeZone;
097    _pattern = pattern;
098    configure();
099  }
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}