Coverage report

  %line %branch
org.apache.commons.validator.DateValidator
0% 
0% 

 1  
 /*
 2  
  * $Id: DateValidator.java 366933 2006-01-07 22:51:01Z niallp $
 3  
  * $Rev: 366933 $
 4  
  * $Date: 2006-01-07 22:51:01 +0000 (Sat, 07 Jan 2006) $
 5  
  *
 6  
  * ====================================================================
 7  
  * Copyright 2001-2006 The Apache Software Foundation
 8  
  *
 9  
  * Licensed under the Apache License, Version 2.0 (the "License");
 10  
  * you may not use this file except in compliance with the License.
 11  
  * You may obtain a copy of the License at
 12  
  *
 13  
  *     http://www.apache.org/licenses/LICENSE-2.0
 14  
  *
 15  
  * Unless required by applicable law or agreed to in writing, software
 16  
  * distributed under the License is distributed on an "AS IS" BASIS,
 17  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18  
  * See the License for the specific language governing permissions and
 19  
  * limitations under the License.
 20  
  */
 21  
 
 22  
 package org.apache.commons.validator;
 23  
 
 24  
 import java.text.DateFormat;
 25  
 import java.text.ParseException;
 26  
 import java.text.SimpleDateFormat;
 27  
 import java.util.Locale;
 28  
 
 29  
 /**
 30  
  * <p>Perform date validations.</p>
 31  
  * <p>
 32  
  * This class is a Singleton; you can retrieve the instance via the
 33  
  * getInstance() method.
 34  
  * </p>
 35  
  *
 36  
  * @since Validator 1.1
 37  
  */
 38  
 public class DateValidator {
 39  
 
 40  
     /**
 41  
      * Singleton instance of this class.
 42  
      */
 43  0
     private static final DateValidator DATE_VALIDATOR = new DateValidator();
 44  
 
 45  
     /**
 46  
      * Returns the Singleton instance of this validator.
 47  
      * @return A singleton instance of the DateValidator.
 48  
      */
 49  
     public static DateValidator getInstance() {
 50  0
         return DATE_VALIDATOR;
 51  
     }
 52  
 
 53  
     /**
 54  
      * Protected constructor for subclasses to use.
 55  
      */
 56  
     protected DateValidator() {
 57  0
         super();
 58  0
     }
 59  
 
 60  
     /**
 61  
      * <p>Checks if the field is a valid date.  The pattern is used with
 62  
      * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
 63  
      * length will be checked so '2/12/1999' will not pass validation with
 64  
      * the format 'MM/dd/yyyy' because the month isn't two digits.
 65  
      * The setLenient method is set to <code>false</code> for all.</p>
 66  
      *
 67  
      * @param value The value validation is being performed on.
 68  
      * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
 69  
      * @param strict Whether or not to have an exact match of the datePattern.
 70  
      * @return true if the date is valid.
 71  
      */
 72  
     public boolean isValid(String value, String datePattern, class="keyword">boolean strict) {
 73  
 
 74  0
         if (value == null
 75  
                 || datePattern == null
 76  
                 || datePattern.length() <= 0) {
 77  
 
 78  0
             return false;
 79  
         }
 80  
 
 81  0
         SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
 82  0
         formatter.setLenient(false);
 83  
 
 84  
         try {
 85  0
             formatter.parse(value);
 86  0
         } catch(ParseException e) {
 87  0
             return false;
 88  
         }
 89  
 
 90  0
         if (strict && (datePattern.length() != value.length())) {
 91  0
             return false;
 92  
         }
 93  
 
 94  0
         return true;
 95  
     }
 96  
 
 97  
     /**
 98  
      * <p>Checks if the field is a valid date.  The <code>Locale</code> is
 99  
      * used with <code>java.text.DateFormat</code>.  The setLenient method
 100  
      * is set to <code>false</code> for all.</p>
 101  
      *
 102  
      * @param value The value validation is being performed on.
 103  
      * @param locale The locale to use for the date format, defaults to the default
 104  
      * system default if null.
 105  
      * @return true if the date is valid.
 106  
      */
 107  
     public boolean isValid(String value, Locale locale) {
 108  
 
 109  0
         if (value == null) {
 110  0
             return false;
 111  
         }
 112  
 
 113  0
         DateFormat formatter = null;
 114  0
         if (locale != null) {
 115  0
             formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
 116  
         } else {
 117  0
             formatter =
 118  
                     DateFormat.getDateInstance(
 119  
                             DateFormat.SHORT,
 120  
                             Locale.getDefault());
 121  
         }
 122  
 
 123  0
         formatter.setLenient(false);
 124  
 
 125  
         try {
 126  0
             formatter.parse(value);
 127  0
         } catch(ParseException e) {
 128  0
             return false;
 129  
         }
 130  
 
 131  0
         return true;
 132  
     }
 133  
 
 134  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.