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