Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DateValidator |
|
| 4.5;4.5 |
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; | |
18 | ||
19 | import java.text.DateFormat; | |
20 | import java.text.ParseException; | |
21 | import java.text.SimpleDateFormat; | |
22 | import java.util.Locale; | |
23 | ||
24 | /** | |
25 | * <p>Perform date validations.</p> | |
26 | * <p> | |
27 | * This class is a Singleton; you can retrieve the instance via the | |
28 | * getInstance() method. | |
29 | * </p> | |
30 | * | |
31 | * @version $Revision: 1739358 $ | |
32 | * @since Validator 1.1 | |
33 | * @deprecated Use the new DateValidator, CalendarValidator or TimeValidator in the | |
34 | * routines package. This class will be removed in a future release. | |
35 | */ | |
36 | @Deprecated | |
37 | public class DateValidator { | |
38 | ||
39 | /** | |
40 | * Singleton instance of this class. | |
41 | */ | |
42 | 0 | private static final DateValidator DATE_VALIDATOR = new DateValidator(); |
43 | ||
44 | /** | |
45 | * Returns the Singleton instance of this validator. | |
46 | * @return A singleton instance of the DateValidator. | |
47 | */ | |
48 | public static DateValidator getInstance() { | |
49 | 0 | return DATE_VALIDATOR; |
50 | } | |
51 | ||
52 | /** | |
53 | * Protected constructor for subclasses to use. | |
54 | */ | |
55 | protected DateValidator() { | |
56 | 0 | super(); |
57 | 0 | } |
58 | ||
59 | /** | |
60 | * <p>Checks if the field is a valid date. The pattern is used with | |
61 | * <code>java.text.SimpleDateFormat</code>. If strict is true, then the | |
62 | * length will be checked so '2/12/1999' will not pass validation with | |
63 | * the format 'MM/dd/yyyy' because the month isn't two digits. | |
64 | * The setLenient method is set to <code>false</code> for all.</p> | |
65 | * | |
66 | * @param value The value validation is being performed on. | |
67 | * @param datePattern The pattern passed to <code>SimpleDateFormat</code>. | |
68 | * @param strict Whether or not to have an exact match of the datePattern. | |
69 | * @return true if the date is valid. | |
70 | */ | |
71 | public boolean isValid(String value, String datePattern, boolean strict) { | |
72 | ||
73 | 0 | if (value == null |
74 | || datePattern == null | |
75 | || datePattern.length() <= 0) { | |
76 | ||
77 | 0 | return false; |
78 | } | |
79 | ||
80 | 0 | SimpleDateFormat formatter = new SimpleDateFormat(datePattern); |
81 | 0 | formatter.setLenient(false); |
82 | ||
83 | try { | |
84 | 0 | formatter.parse(value); |
85 | 0 | } catch(ParseException e) { |
86 | 0 | return false; |
87 | 0 | } |
88 | ||
89 | 0 | if (strict && (datePattern.length() != value.length())) { |
90 | 0 | return false; |
91 | } | |
92 | ||
93 | 0 | return true; |
94 | } | |
95 | ||
96 | /** | |
97 | * <p>Checks if the field is a valid date. The <code>Locale</code> is | |
98 | * used with <code>java.text.DateFormat</code>. The setLenient method | |
99 | * is set to <code>false</code> for all.</p> | |
100 | * | |
101 | * @param value The value validation is being performed on. | |
102 | * @param locale The locale to use for the date format, defaults to the default | |
103 | * system default if null. | |
104 | * @return true if the date is valid. | |
105 | */ | |
106 | public boolean isValid(String value, Locale locale) { | |
107 | ||
108 | 0 | if (value == null) { |
109 | 0 | return false; |
110 | } | |
111 | ||
112 | 0 | DateFormat formatter = null; |
113 | 0 | if (locale != null) { |
114 | 0 | formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale); |
115 | } else { | |
116 | 0 | formatter = |
117 | DateFormat.getDateInstance( | |
118 | DateFormat.SHORT, | |
119 | Locale.getDefault()); | |
120 | } | |
121 | ||
122 | 0 | formatter.setLenient(false); |
123 | ||
124 | try { | |
125 | 0 | formatter.parse(value); |
126 | 0 | } catch(ParseException e) { |
127 | 0 | return false; |
128 | 0 | } |
129 | ||
130 | 0 | return true; |
131 | } | |
132 | ||
133 | } |