1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29
30
31
32
33
34
35
36
37
38 public class DateFormatManager {
39
40
41
42
43
44
45
46
47
48
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
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
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
154
155 public synchronized void setPattern(String pattern) {
156 _pattern = pattern;
157 configure();
158 }
159
160
161
162
163
164
165 public synchronized String getOutputFormat() {
166 return _pattern;
167 }
168
169
170
171
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
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
203
204 public Date parse(String date) throws ParseException {
205 return getDateFormatInstance().parse(date);
206 }
207
208
209
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
223
224
225
226
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
241
242
243 }