001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import java.text.DateFormat;
020: import java.text.ParseException;
021: import java.text.SimpleDateFormat;
022: import java.util.Locale;
023:
024: /**
025: * <p>Perform date validations.</p>
026: * <p>
027: * This class is a Singleton; you can retrieve the instance via the
028: * getInstance() method.
029: * </p>
030: *
031: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
032: * @since Validator 1.1
033: */
034: public class DateValidator {
035:
036: /**
037: * Singleton instance of this class.
038: */
039: private static final DateValidator DATE_VALIDATOR = new DateValidator();
040:
041: /**
042: * Returns the Singleton instance of this validator.
043: * @return A singleton instance of the DateValidator.
044: */
045: public static DateValidator getInstance() {
046: return DATE_VALIDATOR;
047: }
048:
049: /**
050: * Protected constructor for subclasses to use.
051: */
052: protected DateValidator() {
053: super ();
054: }
055:
056: /**
057: * <p>Checks if the field is a valid date. The pattern is used with
058: * <code>java.text.SimpleDateFormat</code>. If strict is true, then the
059: * length will be checked so '2/12/1999' will not pass validation with
060: * the format 'MM/dd/yyyy' because the month isn't two digits.
061: * The setLenient method is set to <code>false</code> for all.</p>
062: *
063: * @param value The value validation is being performed on.
064: * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
065: * @param strict Whether or not to have an exact match of the datePattern.
066: * @return true if the date is valid.
067: */
068: public boolean isValid(String value, String datePattern,
069: boolean strict) {
070:
071: if (value == null || datePattern == null
072: || datePattern.length() <= 0) {
073:
074: return false;
075: }
076:
077: SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
078: formatter.setLenient(false);
079:
080: try {
081: formatter.parse(value);
082: } catch (ParseException e) {
083: return false;
084: }
085:
086: if (strict && (datePattern.length() != value.length())) {
087: return false;
088: }
089:
090: return true;
091: }
092:
093: /**
094: * <p>Checks if the field is a valid date. The <code>Locale</code> is
095: * used with <code>java.text.DateFormat</code>. The setLenient method
096: * is set to <code>false</code> for all.</p>
097: *
098: * @param value The value validation is being performed on.
099: * @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,
112: locale);
113: } else {
114: formatter = DateFormat.getDateInstance(DateFormat.SHORT,
115: Locale.getDefault());
116: }
117:
118: formatter.setLenient(false);
119:
120: try {
121: formatter.parse(value);
122: } catch (ParseException e) {
123: return false;
124: }
125:
126: return true;
127: }
128:
129: }
|