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