当前位置: 首页>>代码示例>>Java>>正文


Java GenericTypeValidator.formatDate方法代码示例

本文整理汇总了Java中org.apache.commons.validator.GenericTypeValidator.formatDate方法的典型用法代码示例。如果您正苦于以下问题:Java GenericTypeValidator.formatDate方法的具体用法?Java GenericTypeValidator.formatDate怎么用?Java GenericTypeValidator.formatDate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.validator.GenericTypeValidator的用法示例。


在下文中一共展示了GenericTypeValidator.formatDate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: validateDate

import org.apache.commons.validator.GenericTypeValidator; //导入方法依赖的package包/类
/**
 *  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.
 * @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);

    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) {
        log.error(e.getMessage(), e);
    }

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

    return result == null ? Boolean.FALSE : result;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:60,代码来源:FieldChecks.java

示例2: validateDate

import org.apache.commons.validator.GenericTypeValidator; //导入方法依赖的package包/类
/**
 * 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.
 * @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;
}
 
开发者ID:SonarSource,项目名称:sonar-scanner-maven,代码行数:71,代码来源:FieldChecks.java


注:本文中的org.apache.commons.validator.GenericTypeValidator.formatDate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。