KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > DateValidator


1 /*
2  * $Id: DateValidator.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 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 JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Locale JavaDoc;
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 JavaDoc value, String JavaDoc datePattern, boolean strict) {
71
72         if (value == null
73                 || datePattern == null
74                 || datePattern.length() <= 0) {
75
76             return false;
77         }
78
79         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(datePattern);
80         formatter.setLenient(false);
81
82         try {
83             formatter.parse(value);
84         } catch(ParseException JavaDoc 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 JavaDoc value, Locale JavaDoc locale) {
105
106         if (value == null) {
107             return false;
108         }
109
110         DateFormat JavaDoc 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 JavaDoc e) {
125             return false;
126         }
127
128         return true;
129     }
130
131 }
132
Popular Tags