public static java.util.Date str2date(String s, String pattern){
        
        SimpleDateFormat df = (SimpleDateFormat)DateFormat.getDateInstance();        
        synchronized(df) {
        
            df.applyPattern(pattern);        
            df.setLenient(false);            try {
        
                java.util.Date date = df.parse(s);                return date;            } catch(Exception e) {
            }
        }
        
        return null;
    }不返回null就是正确的日期。
pattern是(yyyyMMdd, yyyy-MM-dd and so on)

解决方案 »

  1.   

    /**
         *  <p>
         *
         *  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.
         *  </p>
         *
         *@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>ActionErrors</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,
                                        ActionErrors errors,
                                        HttpServletRequest request) {        Date result = null;
            String value = null;
            if (isString(bean)) {
                value = (String) bean;
            } else {
                value = ValidatorUtil.getValueAsString(bean, field.getProperty());
            }
            String datePattern = field.getVarValue("datePattern");
            String datePatternStrict = field.getVarValue("datePatternStrict");
            Locale locale = Resources.getLocale(request);        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);
                    } else {
                        result = GenericTypeValidator.formatDate(value, locale);
                    }
                } catch (Exception e) {
                    LOG.error(e.getMessage(), e);
                }            if (result == null) {
                    errors.add(field.getKey(), Resources.getActionError(request, va, field));
                }
            }        return result;
        }
    我抄的:)
      

  2.   

    其实也就GenericTypeValidator.formatDate(value, datePattern, false);有用
      

  3.   

    to: scbb(星际Baby)
    不是不好,谢谢你的回答
    我只是想知道还有没有其它方法论 :)