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