Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DateValidator |
|
| 1.0526315789473684;1.053 |
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.commons.validator.routines; | |
18 | ||
19 | import java.text.DateFormat; | |
20 | import java.text.Format; | |
21 | import java.util.Calendar; | |
22 | import java.util.Date; | |
23 | import java.util.Locale; | |
24 | import java.util.TimeZone; | |
25 | ||
26 | /** | |
27 | * <p><b>Date Validation</b> and Conversion routines (<code>java.util.Date</code>).</p> | |
28 | * | |
29 | * <p>This validator provides a number of methods for validating/converting | |
30 | * a <code>String</code> date value to a <code>java.util.Date</code> using | |
31 | * <code>java.text.DateFormat</code> to parse either:</p> | |
32 | * <ul> | |
33 | * <li>using the default format for the default <code>Locale</code></li> | |
34 | * <li>using a specified pattern with the default <code>Locale</code></li> | |
35 | * <li>using the default format for a specified <code>Locale</code></li> | |
36 | * <li>using a specified pattern with a specified <code>Locale</code></li> | |
37 | * </ul> | |
38 | * | |
39 | * <p>For each of the above mechanisms, conversion method (i.e the | |
40 | * <code>validate</code> methods) implementations are provided which | |
41 | * either use the default <code>TimeZone</code> or allow the | |
42 | * <code>TimeZone</code> to be specified.</p> | |
43 | * | |
44 | * <p>Use one of the <code>isValid()</code> methods to just validate or | |
45 | * one of the <code>validate()</code> methods to validate and receive a | |
46 | * <i>converted</i> <code>Date</code> value.</p> | |
47 | * | |
48 | * <p>Implementations of the <code>validate()</code> method are provided | |
49 | * to create <code>Date</code> objects for different <i>time zones</i> | |
50 | * if the system default is not appropriate.</p> | |
51 | * | |
52 | * <p>Once a value has been successfully converted the following | |
53 | * methods can be used to perform various date comparison checks:</p> | |
54 | * <ul> | |
55 | * <li><code>compareDates()</code> compares the day, month and | |
56 | * year of two dates, returning 0, -1 or +1 indicating | |
57 | * whether the first date is equal, before or after the second.</li> | |
58 | * <li><code>compareWeeks()</code> compares the week and | |
59 | * year of two dates, returning 0, -1 or +1 indicating | |
60 | * whether the first week is equal, before or after the second.</li> | |
61 | * <li><code>compareMonths()</code> compares the month and | |
62 | * year of two dates, returning 0, -1 or +1 indicating | |
63 | * whether the first month is equal, before or after the second.</li> | |
64 | * <li><code>compareQuarters()</code> compares the quarter and | |
65 | * year of two dates, returning 0, -1 or +1 indicating | |
66 | * whether the first quarter is equal, before or after the second.</li> | |
67 | * <li><code>compareYears()</code> compares the | |
68 | * year of two dates, returning 0, -1 or +1 indicating | |
69 | * whether the first year is equal, before or after the second.</li> | |
70 | * </ul> | |
71 | * | |
72 | * <p>So that the same mechanism used for parsing an <i>input</i> value | |
73 | * for validation can be used to format <i>output</i>, corresponding | |
74 | * <code>format()</code> methods are also provided. That is you can | |
75 | * format either:</p> | |
76 | * <ul> | |
77 | * <li>using a specified pattern</li> | |
78 | * <li>using the format for a specified <code>Locale</code></li> | |
79 | * <li>using the format for the <i>default</i> <code>Locale</code></li> | |
80 | * </ul> | |
81 | * | |
82 | * @version $Revision: 1739356 $ | |
83 | * @since Validator 1.3.0 | |
84 | */ | |
85 | public class DateValidator extends AbstractCalendarValidator { | |
86 | ||
87 | private static final long serialVersionUID = -3966328400469953190L; | |
88 | ||
89 | 1 | private static final DateValidator VALIDATOR = new DateValidator(); |
90 | ||
91 | /** | |
92 | * Return a singleton instance of this validator. | |
93 | * @return A singleton instance of the DateValidator. | |
94 | */ | |
95 | public static DateValidator getInstance() { | |
96 | 20 | return VALIDATOR; |
97 | } | |
98 | ||
99 | /** | |
100 | * Construct a <i>strict</i> instance with <i>short</i> | |
101 | * date style. | |
102 | */ | |
103 | public DateValidator() { | |
104 | 9 | this(true, DateFormat.SHORT); |
105 | 9 | } |
106 | ||
107 | /** | |
108 | * Construct an instance with the specified <i>strict</i> | |
109 | * and <i>date style</i> parameters. | |
110 | * | |
111 | * @param strict <code>true</code> if strict | |
112 | * <code>Format</code> parsing should be used. | |
113 | * @param dateStyle the date style to use for Locale validation. | |
114 | */ | |
115 | public DateValidator(boolean strict, int dateStyle) { | |
116 | 9 | super(strict, dateStyle, -1); |
117 | 9 | } |
118 | ||
119 | /** | |
120 | * <p>Validate/convert a <code>Date</code> using the default | |
121 | * <code>Locale</code> and <code>TimeZone</code>. | |
122 | * | |
123 | * @param value The value validation is being performed on. | |
124 | * @return The parsed <code>Date</code> if valid or <code>null</code> | |
125 | * if invalid. | |
126 | */ | |
127 | public Date validate(String value) { | |
128 | 2 | return (Date)parse(value, (String)null, (Locale)null, (TimeZone)null); |
129 | } | |
130 | ||
131 | /** | |
132 | * <p>Validate/convert a <code>Date</code> using the specified | |
133 | * <code>TimeZone</code> and default <code>Locale</code>. | |
134 | * | |
135 | * @param value The value validation is being performed on. | |
136 | * @param timeZone The Time Zone used to parse the date, system default if null. | |
137 | * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid. | |
138 | */ | |
139 | public Date validate(String value, TimeZone timeZone) { | |
140 | 1 | return (Date)parse(value, (String)null, (Locale)null, timeZone); |
141 | } | |
142 | ||
143 | /** | |
144 | * <p>Validate/convert a <code>Date</code> using the specified | |
145 | * <i>pattern</i> and default <code>TimeZone</code>. | |
146 | * | |
147 | * @param value The value validation is being performed on. | |
148 | * @param pattern The pattern used to validate the value against, or the | |
149 | * default for the <code>Locale</code> if <code>null</code>. | |
150 | * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid. | |
151 | */ | |
152 | public Date validate(String value, String pattern) { | |
153 | 2 | return (Date)parse(value, pattern, (Locale)null, (TimeZone)null); |
154 | } | |
155 | ||
156 | /** | |
157 | * <p>Validate/convert a <code>Date</code> using the specified | |
158 | * <i>pattern</i> and <code>TimeZone</code>. | |
159 | * | |
160 | * @param value The value validation is being performed on. | |
161 | * @param pattern The pattern used to validate the value against, or the | |
162 | * default for the <code>Locale</code> if <code>null</code>. | |
163 | * @param timeZone The Time Zone used to parse the date, system default if null. | |
164 | * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid. | |
165 | */ | |
166 | public Date validate(String value, String pattern, TimeZone timeZone) { | |
167 | 1 | return (Date)parse(value, pattern, (Locale)null, timeZone); |
168 | } | |
169 | ||
170 | /** | |
171 | * <p>Validate/convert a <code>Date</code> using the specified | |
172 | * <code>Locale</code> and default <code>TimeZone</code>. | |
173 | * | |
174 | * @param value The value validation is being performed on. | |
175 | * @param locale The locale to use for the date format, system default if null. | |
176 | * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid. | |
177 | */ | |
178 | public Date validate(String value, Locale locale) { | |
179 | 2 | return (Date)parse(value, (String)null, locale, (TimeZone)null); |
180 | } | |
181 | ||
182 | /** | |
183 | * <p>Validate/convert a <code>Date</code> using the specified | |
184 | * <code>Locale</code> and <code>TimeZone</code>. | |
185 | * | |
186 | * @param value The value validation is being performed on. | |
187 | * @param locale The locale to use for the date format, system default if null. | |
188 | * @param timeZone The Time Zone used to parse the date, system default if null. | |
189 | * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid. | |
190 | */ | |
191 | public Date validate(String value, Locale locale, TimeZone timeZone) { | |
192 | 1 | return (Date)parse(value, (String)null, locale, timeZone); |
193 | } | |
194 | ||
195 | /** | |
196 | * <p>Validate/convert a <code>Date</code> using the specified pattern | |
197 | * and <code>Locale</code> and the default <code>TimeZone</code>. | |
198 | * | |
199 | * @param value The value validation is being performed on. | |
200 | * @param pattern The pattern used to validate the value against, or the | |
201 | * default for the <code>Locale</code> if <code>null</code>. | |
202 | * @param locale The locale to use for the date format, system default if null. | |
203 | * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid. | |
204 | */ | |
205 | public Date validate(String value, String pattern, Locale locale) { | |
206 | 2 | return (Date)parse(value, pattern, locale, (TimeZone)null); |
207 | } | |
208 | ||
209 | /** | |
210 | * <p>Validate/convert a <code>Date</code> using the specified | |
211 | * pattern, and <code>Locale</code> and <code>TimeZone</code>. | |
212 | * | |
213 | * @param value The value validation is being performed on. | |
214 | * @param pattern The pattern used to validate the value against, or the | |
215 | * default for the <code>Locale</code> if <code>null</code>. | |
216 | * @param locale The locale to use for the date format, system default if null. | |
217 | * @param timeZone The Time Zone used to parse the date, system default if null. | |
218 | * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid. | |
219 | */ | |
220 | public Date validate(String value, String pattern, Locale locale, TimeZone timeZone) { | |
221 | 1 | return (Date)parse(value, pattern, locale, timeZone); |
222 | } | |
223 | ||
224 | /** | |
225 | * <p>Compare Dates (day, month and year - not time).</p> | |
226 | * | |
227 | * @param value The <code>Calendar</code> value to check. | |
228 | * @param compare The <code>Calendar</code> to compare the value to. | |
229 | * @param timeZone The Time Zone used to compare the dates, system default if null. | |
230 | * @return Zero if the dates are equal, -1 if first | |
231 | * date is less than the seconds and +1 if the first | |
232 | * date is greater than. | |
233 | */ | |
234 | public int compareDates(Date value, Date compare, TimeZone timeZone) { | |
235 | 7 | Calendar calendarValue = getCalendar(value, timeZone); |
236 | 7 | Calendar calendarCompare = getCalendar(compare, timeZone); |
237 | 7 | return compare(calendarValue, calendarCompare, Calendar.DATE); |
238 | } | |
239 | ||
240 | /** | |
241 | * <p>Compare Weeks (week and year).</p> | |
242 | * | |
243 | * @param value The <code>Date</code> value to check. | |
244 | * @param compare The <code>Date</code> to compare the value to. | |
245 | * @param timeZone The Time Zone used to compare the dates, system default if null. | |
246 | * @return Zero if the weeks are equal, -1 if first | |
247 | * parameter's week is less than the seconds and +1 if the first | |
248 | * parameter's week is greater than. | |
249 | */ | |
250 | public int compareWeeks(Date value, Date compare, TimeZone timeZone) { | |
251 | 5 | Calendar calendarValue = getCalendar(value, timeZone); |
252 | 5 | Calendar calendarCompare = getCalendar(compare, timeZone); |
253 | 5 | return compare(calendarValue, calendarCompare, Calendar.WEEK_OF_YEAR); |
254 | } | |
255 | ||
256 | /** | |
257 | * <p>Compare Months (month and year).</p> | |
258 | * | |
259 | * @param value The <code>Date</code> value to check. | |
260 | * @param compare The <code>Date</code> to compare the value to. | |
261 | * @param timeZone The Time Zone used to compare the dates, system default if null. | |
262 | * @return Zero if the months are equal, -1 if first | |
263 | * parameter's month is less than the seconds and +1 if the first | |
264 | * parameter's month is greater than. | |
265 | */ | |
266 | public int compareMonths(Date value, Date compare, TimeZone timeZone) { | |
267 | 5 | Calendar calendarValue = getCalendar(value, timeZone); |
268 | 5 | Calendar calendarCompare = getCalendar(compare, timeZone); |
269 | 5 | return compare(calendarValue, calendarCompare, Calendar.MONTH); |
270 | } | |
271 | ||
272 | /** | |
273 | * <p>Compare Quarters (quarter and year).</p> | |
274 | * | |
275 | * @param value The <code>Date</code> value to check. | |
276 | * @param compare The <code>Date</code> to compare the value to. | |
277 | * @param timeZone The Time Zone used to compare the dates, system default if null. | |
278 | * @return Zero if the months are equal, -1 if first | |
279 | * parameter's quarter is less than the seconds and +1 if the first | |
280 | * parameter's quarter is greater than. | |
281 | */ | |
282 | public int compareQuarters(Date value, Date compare, TimeZone timeZone) { | |
283 | 6 | return compareQuarters(value, compare, timeZone, 1); |
284 | } | |
285 | ||
286 | /** | |
287 | * <p>Compare Quarters (quarter and year).</p> | |
288 | * | |
289 | * @param value The <code>Date</code> value to check. | |
290 | * @param compare The <code>Date</code> to compare the value to. | |
291 | * @param timeZone The Time Zone used to compare the dates, system default if null. | |
292 | * @param monthOfFirstQuarter The month that the first quarter starts. | |
293 | * @return Zero if the quarters are equal, -1 if first | |
294 | * parameter's quarter is less than the seconds and +1 if the first | |
295 | * parameter's quarter is greater than. | |
296 | */ | |
297 | public int compareQuarters(Date value, Date compare, TimeZone timeZone, int monthOfFirstQuarter) { | |
298 | 13 | Calendar calendarValue = getCalendar(value, timeZone); |
299 | 13 | Calendar calendarCompare = getCalendar(compare, timeZone); |
300 | 13 | return super.compareQuarters(calendarValue, calendarCompare, monthOfFirstQuarter); |
301 | } | |
302 | ||
303 | /** | |
304 | * <p>Compare Years.</p> | |
305 | * | |
306 | * @param value The <code>Date</code> value to check. | |
307 | * @param compare The <code>Date</code> to compare the value to. | |
308 | * @param timeZone The Time Zone used to compare the dates, system default if null. | |
309 | * @return Zero if the years are equal, -1 if first | |
310 | * parameter's year is less than the seconds and +1 if the first | |
311 | * parameter's year is greater than. | |
312 | */ | |
313 | public int compareYears(Date value, Date compare, TimeZone timeZone) { | |
314 | 3 | Calendar calendarValue = getCalendar(value, timeZone); |
315 | 3 | Calendar calendarCompare = getCalendar(compare, timeZone); |
316 | 3 | return compare(calendarValue, calendarCompare, Calendar.YEAR); |
317 | } | |
318 | ||
319 | /** | |
320 | * <p>Returns the parsed <code>Date</code> unchanged.</p> | |
321 | * | |
322 | * @param value The parsed <code>Date</code> object created. | |
323 | * @param formatter The Format used to parse the value with. | |
324 | * @return The parsed value converted to a <code>Calendar</code>. | |
325 | */ | |
326 | @Override | |
327 | protected Object processParsedValue(Object value, Format formatter) { | |
328 | 41 | return value; |
329 | } | |
330 | ||
331 | /** | |
332 | * <p>Convert a <code>Date</code> to a <code>Calendar</code>.</p> | |
333 | * | |
334 | * @param value The date value to be converted. | |
335 | * @return The converted <code>Calendar</code>. | |
336 | */ | |
337 | private Calendar getCalendar(Date value, TimeZone timeZone) { | |
338 | ||
339 | 66 | Calendar calendar = null; |
340 | 66 | if (timeZone != null) { |
341 | 66 | calendar = Calendar.getInstance(timeZone); |
342 | } else { | |
343 | 0 | calendar = Calendar.getInstance(); |
344 | } | |
345 | 66 | calendar.setTime(value); |
346 | 66 | return calendar; |
347 | ||
348 | } | |
349 | ||
350 | } |