DateValidator.java

  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. import java.text.DateFormat;
  19. import java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Locale;

  22. /**
  23.  * <p>Perform date validations.</p>
  24.  * <p>
  25.  * This class is a Singleton; you can retrieve the instance via the
  26.  * getInstance() method.
  27.  * </p>
  28.  *
  29.  * @since 1.1
  30.  * @deprecated Use the new DateValidator, CalendarValidator or TimeValidator in the
  31.  * routines package. This class will be removed in a future release.
  32.  */
  33. @Deprecated
  34. public class DateValidator {

  35.     /**
  36.      * Singleton instance of this class.
  37.      */
  38.     private static final DateValidator DATE_VALIDATOR = new DateValidator();

  39.     /**
  40.      * Returns the Singleton instance of this validator.
  41.      * @return A singleton instance of the DateValidator.
  42.      */
  43.     public static DateValidator getInstance() {
  44.         return DATE_VALIDATOR;
  45.     }

  46.     /**
  47.      * Protected constructor for subclasses to use.
  48.      */
  49.     protected DateValidator() {
  50.     }

  51.     /**
  52.      * <p>Checks if the field is a valid date.  The <code>Locale</code> is
  53.      * used with <code>java.text.DateFormat</code>.  The setLenient method
  54.      * is set to {@code false} for all.</p>
  55.      *
  56.      * @param value The value validation is being performed on.
  57.      * @param locale The locale to use for the date format, defaults to the default
  58.      * system default if null.
  59.      * @return true if the date is valid.
  60.      */
  61.     public boolean isValid(final String value, final Locale locale) {

  62.         if (value == null) {
  63.             return false;
  64.         }

  65.         DateFormat formatter;
  66.         if (locale != null) {
  67.             formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
  68.         } else {
  69.             formatter =
  70.                     DateFormat.getDateInstance(
  71.                             DateFormat.SHORT,
  72.                             Locale.getDefault());
  73.         }

  74.         formatter.setLenient(false);

  75.         try {
  76.             formatter.parse(value);
  77.         } catch (final ParseException e) {
  78.             return false;
  79.         }

  80.         return true;
  81.     }

  82.     /**
  83.      * <p>Checks if the field is a valid date.  The pattern is used with
  84.      * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
  85.      * length will be checked so '2/12/1999' will not pass validation with
  86.      * the format 'MM/dd/yyyy' because the month isn't two digits.
  87.      * The setLenient method is set to {@code false} for all.</p>
  88.      *
  89.      * @param value The value validation is being performed on.
  90.      * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
  91.      * @param strict Whether or not to have an exact match of the datePattern.
  92.      * @return true if the date is valid.
  93.      */
  94.     public boolean isValid(final String value, final String datePattern, final boolean strict) {

  95.         if (value == null
  96.                 || datePattern == null
  97.                 || datePattern.isEmpty()) {

  98.             return false;
  99.         }

  100.         final SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
  101.         formatter.setLenient(false);

  102.         try {
  103.             formatter.parse(value);
  104.         } catch (final ParseException e) {
  105.             return false;
  106.         }

  107.         if (strict && datePattern.length() != value.length()) {
  108.             return false;
  109.         }

  110.         return true;
  111.     }

  112. }