Perform date validations.
** This class is a Singleton; you can retrieve the instance via the * getInstance() method. *
* * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $ * @since Validator 1.1 */ public class DateValidator { /** * Singleton instance of this class. */ private static final DateValidator DATE_VALIDATOR = new DateValidator(); /** * Returns the Singleton instance of this validator. * @return A singleton instance of the DateValidator. */ public static DateValidator getInstance() { return DATE_VALIDATOR; } /** * Protected constructor for subclasses to use. */ protected DateValidator() { super(); } /** *Checks if the field is a valid date. The pattern is used with
* java.text.SimpleDateFormat
. If strict is true, then the
* length will be checked so '2/12/1999' will not pass validation with
* the format 'MM/dd/yyyy' because the month isn't two digits.
* The setLenient method is set to false
for all.
SimpleDateFormat
.
* @param strict Whether or not to have an exact match of the datePattern.
* @return true if the date is valid.
*/
public boolean isValid(String value, String datePattern, boolean strict) {
if (value == null
|| datePattern == null
|| datePattern.length() <= 0) {
return false;
}
SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
formatter.setLenient(false);
try {
formatter.parse(value);
} catch(ParseException e) {
return false;
}
if (strict && (datePattern.length() != value.length())) {
return false;
}
return true;
}
/**
* Checks if the field is a valid date. The Locale
is
* used with java.text.DateFormat
. The setLenient method
* is set to false
for all.