1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package nl.openedge.baritus.converters;
33
34 import java.text.DateFormat;
35 import java.text.ParseException;
36 import java.text.SimpleDateFormat;
37 import java.util.Date;
38 import java.util.Locale;
39
40 /***
41 * <p>Standard {@link LocaleConverter}
42 * implementation that converts an incoming
43 * locale-sensitive String into a <code>java.util.Date</code> object,
44 * optionally using a default value or throwing a
45 * {@link ConversionException}
46 * if a conversion error occurs.</p>
47 *
48 * @author Yauheny Mikulski
49 * @author Michael Szlapa
50 */
51
52 public class DateLocaleConverter extends BaseLocaleConverter
53 {
54
55 private boolean lenient = false;
56 private int dateStyle = DateFormat.SHORT;
57
58
59
60 /***
61 * Create a {@link LocaleConverter}
62 * that will throw a {@link ConversionException}
63 * if a conversion error occurs. The locale is the default locale for
64 * this instance of the Java Virtual Machine and an unlocalized pattern is used
65 * for the convertion.
66 *
67 */
68 public DateLocaleConverter()
69 {
70
71 this(Locale.getDefault());
72 }
73
74 /***
75 * Create a {@link LocaleConverter}
76 * that will throw a {@link ConversionException}
77 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
78 *
79 * @param locale The locale
80 */
81 public DateLocaleConverter(Locale locale)
82 {
83
84 this(locale, null);
85 }
86
87 /***
88 * Create a {@link LocaleConverter}
89 * that will throw a {@link ConversionException}
90 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
91 *
92 * @param locale The locale
93 * @param pattern The convertion pattern
94 */
95 public DateLocaleConverter(Locale locale, String pattern)
96 {
97
98 super(locale, pattern, false);
99 }
100
101
102
103 /***
104 * Returns whether date formatting is lenient.
105 *
106 * @return true if the <code>DateFormat</code> used for formatting is lenient
107 * @see java.text.DateFormat#isLenient
108 */
109 public boolean isLenient()
110 {
111 return lenient;
112 }
113
114 /***
115 * Specify whether or not date-time parsing should be lenient.
116 *
117 * @param lenient true if the <code>DateFormat</code> used for formatting should be lenient
118 * @see java.text.DateFormat#setLenient
119 */
120 public void setLenient(boolean lenient)
121 {
122 this.lenient = lenient;
123 }
124
125 /***
126 * get date style
127 * @return int date style as a constant from DateFormat
128 */
129 public int getDateStyle()
130 {
131 return dateStyle;
132 }
133
134 /***
135 * set date style
136 * @param dateStyle
137 */
138 public void setDateStyle(int dateStyle)
139 {
140 this.dateStyle = dateStyle;
141 }
142
143
144
145 /***
146 * Convert the specified locale-sensitive input object into an output object of the
147 * specified type.
148 *
149 * @param value The input object to be converted
150 * @param pattern The pattern is used for the convertion
151 *
152 * @exception ConversionException if conversion cannot be performed
153 * successfully
154 */
155 protected Object parse(Object value, String pattern) throws ConversionException
156 {
157 DateFormat formatter = getFormat(pattern, locale);
158
159 try
160 {
161 return formatter.parse((String) value);
162 }
163 catch (ParseException e)
164 {
165 String dpat = null;
166 if(pattern != null)
167 {
168 dpat = pattern;
169 }
170 else if(formatter instanceof SimpleDateFormat)
171 {
172 dpat = ((SimpleDateFormat)formatter).toLocalizedPattern();
173 }
174
175 throw new ConversionException(e, dpat);
176 }
177 }
178
179 /***
180 * format value with pattern or using the default pattern
181 * @see nl.openedge.baritus.converters.Formatter#format(java.lang.Object, java.lang.String)
182 */
183 public String format(Object value, String pattern) throws IllegalArgumentException
184 {
185 DateFormat format = getFormat(pattern, locale);
186 Date date = null;
187 if(value instanceof Date)
188 {
189 date = (Date)value;
190 }
191 else
192 {
193 date = (Date)convert(Date.class, value);
194 }
195 return format.format(date);
196 }
197
198 /***
199 * Get date format
200 */
201 private DateFormat getFormat(String pattern, Locale locale)
202 {
203 DateFormat format = null;
204 if (pattern == null)
205 {
206 format = DateFormat.getDateInstance(dateStyle, locale);
207 }
208 else
209 {
210 SimpleDateFormat _format = new SimpleDateFormat(pattern, locale);
211 _format.setLenient(lenient);
212 if (locPattern)
213 {
214 _format.applyLocalizedPattern(pattern);
215 }
216 else
217 {
218 _format.applyPattern(pattern);
219 }
220 format = _format;
221 }
222 return format;
223 }
224
225 /***
226 * Convert the specified locale-sensitive input object into an output object of the
227 * specified type.
228 *
229 * @param type Data type to which this value should be converted
230 * @param value The input object to be converted
231 * @param pattern The pattern is used for the convertion
232 *
233 * @exception ConversionException if conversion cannot be performed
234 * successfully
235 */
236 public Object convert(Class type, Object value, String pattern)
237 {
238 if (value == null)
239 {
240 return null;
241 }
242
243 return parse(value, pattern);
244 }
245
246 }