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, String datePattern, boolean strict) 

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>.
 *//*ww  w  .j a  va  2s . c  om*/
public static Date validateDate(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");

    Date result = null;
    if (datePattern != null && datePattern.length() > 0) {
        result = GenericTypeValidator.formatDate(value, datePattern, false);
    } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
        result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
    }

    return result;
}

From source file:jp.terasoluna.fw.validation.ValidationUtil.java

/**
 * Date???/* w  w w .  ja v  a  2  s . c o m*/
 * <br>
 * ??????
 * ????????????
 * 2000/02/31????????<code>value</code>
 * ?????<code>null</code>???
 * <ul>
 * <li>
 * <code>datePattern</code>?<code>null</code>????
 * ????<br>
 * ??????
 * ?????<code>datePattern</code>?yyyy/MM/dd???
 * 2000/1/1????2000/01/01?Date???
 * <li>
 * <code>datePatternStrict</code>?<code>null</code>????
 * ????<br>
 * ?????
 * ?????<code>datePattern</code>?yyyy/MM/dd???
 * 2000/1/1????null???
 * <li>
 * <code>datePattern</code>?<code>datePatternStrict</code>?
 * ????<code>null</code>???? ????<br>
 * <code>datePattern</code>????
 * <li>
 * <code>datePattern</code>?<code>datePatternStrict</code>?
 * ???<code>null</code>???????<br>
 * ???
 * </ul>
 * <li>
 * <code>value</code>?<code>null</code>????
 * ???<code>null</code>???
 *
 * @param value ??
 * @param datePattern ???
 * @param datePatternStrict ??
 * @return ????Date???????null
 */
public static Date toDate(String value, String datePattern, String datePatternStrict) {

    if (StringUtils.isEmpty(value)) {
        return null;
    }

    Date result = null;

    // ??????
    if (datePattern != null && datePattern.length() > 0) {
        result = GenericTypeValidator.formatDate(value, datePattern, false);

        // ?????
    } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
        result = GenericTypeValidator.formatDate(value, datePatternStrict, true);

        // ??????
    } else {
        throw new IllegalArgumentException("datePattern or datePatternStrict must be specified.");
    }

    return result;
}

From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java

/**
 * tw`FbN?B//from   w  w w .  j a  v a2s  .  co m
 * 
 * <p>
 * G?[???AG?[????A wG?[?Xg?B G?[?bZ?[WtH?[}bg?A
 * <code>validation-rules.xml</code>L?q?B<br>
 * ?At2000/01/012010/12/31 ???B
 * </p>
 * <h5>validation.xmlL?q</h5>
 * <code><pre>
 * &lt;form name=&quot;/sample&quot;&gt;
 *  ?E?E?E
 *  &lt;field property=&quot;date&quot;
 *      depends=&quot;dateRange&quot;&gt;
 *    &lt;arg key=&quot;label.date&quot; position=&quot;0&quot;/&gt;
 *    &lt;var&gt;
 *      &lt;var-name&gt;startDate&lt;/var-name&gt;
 *      &lt;var-value&gt;2000/01/01&lt;/var-value&gt;
 *    &lt;/var&gt;
 *    &lt;var&gt;
 *      &lt;var-name&gt;endDate&lt;/var-name&gt;
 *      &lt;var-value&gt;2010/12/31&lt;/var-value&gt;
 *    &lt;/var&gt;
 *    &lt;var&gt;
 *      &lt;var-name&gt;datePattern&lt;/var-name&gt;
 *      &lt;var-value&gt;yyyy/MM/dd&lt;/var-value&gt;
 *    &lt;/var&gt;
 *  &lt;/field&gt;
 *  ?E?E?E
 * &lt;/form&gt;
 * </pre></code>
 * <h5>validation.xml?v&lt;var&gt;vf</h5>
 * <table border="1">
 * <tr>
 * <td><center><b><code>var-name</code></b></center></td>
 * <td><center><b><code>var-value</code></b></center></td>
 * <td><center><b>K?{?</b></center></td>
 * <td><center><b>Tv</b></center></td>
 * </tr>
 * <tr>
 * <td> startDate </td>
 * <td>Jnt</td>
 * <td>false</td>
 * <td>tJnlt?B
 * datePatterndatePatternStrictwttH?[}bgv?B</td>
 * </tr>
 * <tr>
 * <td> endDate </td>
 * <td>?It</td>
 * <td>false</td>
 * <td>t?Ilt?B
 * datePatterndatePatternStrictwttH?[}bgv?B</td>
 * </tr>
 * <tr>
 * <td> datePattern </td>
 * <td>tp^?[</td>
 * <td>false</td>
 * <td>tp^?[?BDate^tH?[}bg?[?]?B</td>
 * </tr>
 * <tr>
 * <td> datePatternStrict </td>
 * <td>tp^?[?BDate^tH?[}bg?[?]?B</td>
 * <td>false</td>
 * <td>tp^?[`FbN?s?B tp^?[yyyy/MM/dd???A2001/1/1G?[?B
 * datePatternw???AdatePatternwtH?[}bg D??B</td>
 * </tr>
 * </table>
 * 
 * @param bean
 *            ??IuWFNg
 * @param va
 *            ValidatorpValidatorAction
 * @param field
 *            tB?[hIuWFNg
 * @param errors
 *            ANVG?[
 * @param validator
 *            ValidatorCX^X
 * @param request
 *            HTTPNGXg
 * @return l? <code>true</code>
 */
public static boolean validateDateRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {

    // beannull?AG?[?O?o?Atruep?B
    if (bean == null) {
        log.error("bean is null.");
        return true;
    }

    String value = null;
    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }
    // ?lnull?Atruep
    if (GenericValidator.isBlankOrNull(value)) {
        return true;
    }

    // t?`FbN
    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");
    Locale locale = RequestUtils.getUserLocale(request, null);
    Date result = null;
    try {
        if (datePattern != null && datePattern.length() > 0) {
            result = GenericTypeValidator.formatDate(value, datePattern, false);
        } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
            result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
        } else {
            result = GenericTypeValidator.formatDate(value, locale);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

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

    // t`FbN
    // Jn
    String startDateStr = field.getVarValue("startDate");
    // ?I
    String endDateStr = field.getVarValue("endDate");

    if (startDateStr == null && endDateStr == null) {
        // tw????
        return true;
    }

    // Jnt?~`FbN
    if (startDateStr != null && startDateStr.length() > 0) {
        Date startDate = null;

        if (datePattern != null && datePattern.length() > 0) {
            startDate = GenericTypeValidator.formatDate(startDateStr, datePattern, false);
        } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
            startDate = GenericTypeValidator.formatDate(startDateStr, datePatternStrict, true);
        } else {
            startDate = GenericTypeValidator.formatDate(startDateStr, locale);
        }

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

    // ?ItO`FbN
    if (endDateStr != null && endDateStr.length() > 0) {
        Date endDate = null;

        if (datePattern != null && datePattern.length() > 0) {
            endDate = GenericTypeValidator.formatDate(endDateStr, datePattern, false);
        } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
            endDate = GenericTypeValidator.formatDate(endDateStr, datePatternStrict, true);
        } else {
            endDate = GenericTypeValidator.formatDate(endDateStr, locale);
        }

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

    return true;
}

From source file:org.megatome.frame2.validator.CommonsFieldValidator.java

/**
 * Validate that a value can be converted to a date value.
 * //from   www.  j  a  va 2s. co  m
 * @param value
 *            The value to validate
 * @param va
 *            The validator action
 * @param errors
 *            Errors object to populate
 * @param field
 *            The field to validate
 * @return The Date value of the field, or null if it could not be
 *         converted.
 */
public static Date validateDate(String value, ValidatorAction va, Errors errors, Field field) {
    if (value == null) {
        return null;
    }
    String datePattern = field.getVarValue("datePattern"); //$NON-NLS-1$
    String datePatternStrict = field.getVarValue("datePatternStrict"); //$NON-NLS-1$
    Date result = null;
    try {
        if (datePattern != null && datePattern.length() > 0) {
            result = GenericTypeValidator.formatDate(value, datePattern, false);
        } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
            result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
        } else {
            return null;
        }
    } catch (Exception e) {
        LOGGER.info("Date validation failed for value = " + value //$NON-NLS-1$
                + " exception: " + e); //$NON-NLS-1$
    }
    if (result == null) {
        addError(va, errors, field);
    }
    return result;
}

From source file:org.sgrp.singer.validator.ArrayFieldChecks.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.
 *
 * @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.//  w w  w. jav a 2 s.com
 * @param  errors   The <code>ActionMessages</code> object to add errors to if any
 *      validation errors occur.
 * @param  request  Current request object.
 * @return true if valid, false otherwise.
 */
public static Object[] validateDate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request) {

    String[] value = null;
    if (isStringArray(bean)) {
        value = (String[]) bean;
    } else {
        value = new String[0];
    }
    Object[] result = new Object[value.length];

    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");
    Locale locale = RequestUtils.getUserLocale(request, null);

    for (int i = 0; i < value.length; i++) {
        if (GenericValidator.isBlankOrNull(value[i])) {
            result[i] = Boolean.TRUE;
        }

        try {
            if (datePattern != null && datePattern.length() > 0) {
                result[i] = GenericTypeValidator.formatDate(value[i], datePattern, false);
            } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
                result[i] = GenericTypeValidator.formatDate(value[i], datePatternStrict, true);
            } else {
                result[i] = GenericTypeValidator.formatDate(value[i], locale);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        if (result[i] == null) {
            errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
            result[i] = Boolean.FALSE;
        }
    }
    return result;
}

From source file:org.springmodules.commons.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  .  java 2 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>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Date if valid, a null if blank or invalid.
 */
public static Date validateDate(Object bean, ValidatorAction va, Field field, Errors errors) {

    Date result = null;
    String value = extractValue(bean, field);
    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (datePattern != null && datePattern.length() > 0) {
                result = GenericTypeValidator.formatDate(value, datePattern, false);
            } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
                result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        if (result == null) {
            rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:org.springmodules.validation.commons.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 ww  w .ja  va 2s.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>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Date if valid, a null if blank or invalid.
 */
public static Date validateDate(Object bean, ValidatorAction va, Field field, Errors errors) {

    Date result = null;
    String value = FieldChecks.extractValue(bean, field);
    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (datePattern != null && datePattern.length() > 0) {
                result = GenericTypeValidator.formatDate(value, datePattern, false);
            } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
                result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
            }
        } catch (Exception e) {
            FieldChecks.log.error(e.getMessage(), e);
        }

        if (result == null) {
            FieldChecks.rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:us.mn.state.health.lims.common.util.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./*  w  ww.  ja  v a  2 s .com*/
 * 
 * @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;
    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }
    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");
    Locale locale = RequestUtils.getUserLocale(request, null);

    // this is added to use message key
    String datePatternForResources = ResourceLocator.getInstance().getMessageResources().getMessage(locale,
            datePattern);
    if (datePatternForResources != null) {
        datePattern = datePatternForResources;
    }

    datePatternForResources = ResourceLocator.getInstance().getMessageResources().getMessage(locale,
            datePatternStrict);
    if (datePatternForResources != null) {
        datePatternStrict = datePatternForResources;
    }

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

    try {
        if (datePattern != null && datePattern.length() > 0) {
            result = GenericTypeValidator.formatDate(value, datePattern, false);
        } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
            result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
        } else {
            result = GenericTypeValidator.formatDate(value, locale);
        }
    } catch (Exception e) {
        //bugzilla 2154
        LogEvent.logError("FieldChecks", "validateDate()", e.toString());
    }

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

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