View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/DateValidator.java,v 1.6 2004/02/21 17:10:29 rleland Exp $
3    * $Revision: 1.6 $
4    * $Date: 2004/02/21 17:10:29 $
5    *
6    * ====================================================================
7    * Copyright 2001-2004 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      private static final DateValidator instance = new DateValidator();
44  
45      /***
46       * Returns the Singleton instance of this validator.
47       */
48      public static DateValidator getInstance() {
49          return instance;
50      }
51  
52      /***
53       * Protected constructor for subclasses to use.
54       */
55      protected DateValidator() {
56          super();
57      }
58  
59      /***
60       * <p>Checks if the field is a valid date.  The pattern is used with
61       * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
62       * length will be checked so '2/12/1999' will not pass validation with
63       * the format 'MM/dd/yyyy' because the month isn't two digits.
64       * The setLenient method is set to <code>false</code> for all.</p>
65       *
66       * @param value The value validation is being performed on.
67       * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
68       * @param strict Whether or not to have an exact match of the datePattern.
69       */
70      public boolean isValid(String value, String datePattern, boolean strict) {
71  
72          if (value == null
73                  || datePattern == null
74                  || datePattern.length() <= 0) {
75  
76              return false;
77          }
78  
79          SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
80          formatter.setLenient(false);
81  
82          try {
83              formatter.parse(value);
84          } catch(ParseException e) {
85              return false;
86          }
87  
88          if (strict && (datePattern.length() != value.length())) {
89              return false;
90          }
91  
92          return true;
93      }
94  
95      /***
96       * <p>Checks if the field is a valid date.  The <code>Locale</code> is
97       * used with <code>java.text.DateFormat</code>.  The setLenient method
98       * is set to <code>false</code> for all.</p>
99       *
100      * @param value The value validation is being performed on.
101      * @param locale The locale to use for the date format, defaults to the default
102      * system default if null.
103      */
104     public boolean isValid(String value, Locale locale) {
105 
106         if (value == null) {
107             return false;
108         }
109 
110         DateFormat formatter = null;
111         if (locale != null) {
112             formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
113         } else {
114             formatter =
115                     DateFormat.getDateInstance(
116                             DateFormat.SHORT,
117                             Locale.getDefault());
118         }
119 
120         formatter.setLenient(false);
121 
122         try {
123             formatter.parse(value);
124         } catch(ParseException e) {
125             return false;
126         }
127 
128         return true;
129     }
130 
131 }