Example usage for org.apache.commons.validator GenericTypeValidator formatDate

List of usage examples for org.apache.commons.validator GenericTypeValidator formatDate

Introduction

In this page you can find the example usage for org.apache.commons.validator GenericTypeValidator formatDate.

Prototype

public static Date formatDate(String value, Locale locale) 

Source Link

Document

Checks if the field is a valid date.

Usage

From source file:com.core.validators.CommonValidator.java

/**
 * Checks if the field can be successfully converted to a <code>date</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted 
 * to a <code>date</code> <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 *///  w w  w.  j av a  2 s  .c om
public static Date validateDate(Object bean, Field field, Locale locale) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    return GenericTypeValidator.formatDate(value, locale);
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field is a valid date. If the field has a datePattern
 * variable, that will be used to format <code>java.text.SimpleDateFormat</code>.
 * If the field has a datePatternStrict variable, that will be used to
 * format <code>java.text.SimpleDateFormat</code> and 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. If no datePattern
 * variable is specified, then the field gets the DateFormat.SHORT format
 * for the locale. The setLenient method is set to <code>false</code> for
 * all variations.//from  w  w  w.  j  a v a2 s  . c  om
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.
 * @param field     The <code>Field</code> object associated with the
 *                  current field being validated.
 * @param errors    The <code>ActionMessages</code> object to add errors
 *                  to if any validation errors occur.
 * @param validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return true if valid, false otherwise.
 */
public static Object validateDate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object result = null;
    String value = null;

    value = evaluateBean(bean, field);

    boolean isStrict = false;
    String datePattern = Resources.getVarValue("datePattern", field, validator, request, false);

    if (GenericValidator.isBlankOrNull(datePattern)) {
        datePattern = Resources.getVarValue("datePatternStrict", field, validator, request, false);

        if (!GenericValidator.isBlankOrNull(datePattern)) {
            isStrict = true;
        }
    }

    Locale locale = RequestUtils.getUserLocale(request, null);

    if (GenericValidator.isBlankOrNull(value)) {
        return Boolean.TRUE;
    }

    try {
        if (GenericValidator.isBlankOrNull(datePattern)) {
            result = GenericTypeValidator.formatDate(value, locale);
        } else {
            result = GenericTypeValidator.formatDate(value, datePattern, isStrict);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    if (result == null) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
    }

    return (result == null) ? Boolean.FALSE : result;
}