@Override public void setAsText(String text) throws IllegalArgumentException { if (text == null || (text = text.trim()).length() == 0) { return; } try { setValue(DatePatterns.changeType(DatePatterns.parse(text), targetType)); } catch (ParseException e) { throw new IllegalArgumentException(e); } }
public Date resolve(Invocation inv, ParamMetaData metaData) throws Exception { String text = null; for (String paramName : metaData.getParamNames()) { if (paramName != null) { text = inv.getParameter(paramName); if (text != null && (text = text.trim()).length() > 0) { break; } } } Date date = resolveUtilDate(text, metaData); return DatePatterns.changeType(date, metaData.getParamType()); }
protected Date resolveUtilDate(String text, ParamMetaData metaData) throws ParseException { if (StringUtils.isEmpty(text)) { DefValue defaultValudeAnnotation = metaData.getAnnotation(DefValue.class); if (defaultValudeAnnotation != null && !DefValue.NATIVE_DEFAULT.equals(defaultValudeAnnotation.value())) { if (StringUtils.isEmpty(defaultValudeAnnotation.value())) { return new Date(); // 当前时间! } else { text = defaultValudeAnnotation.value(); // 改变要被解析的文本! } } else { return null; // 保留null,而非当前时间 } } Annotation[] paramAnnotations = metaData.getMethod().getParameterAnnotations()[metaData.getIndex()]; for (Annotation annotation : paramAnnotations) { if (annotation instanceof Pattern) { String[] patterns = Pattern.class.cast(annotation).value(); for (String pattern : patterns) { // 以long为时间 if ("long".equals(pattern)) { boolean digit = true; for (int i = 0; i < text.length(); i++) { if (!Character.isDigit(text.charAt(i))) { digit = false; break; } } if (digit) { return new Date(Long.parseLong(text)); } } // 可以配置多个pattern!! 通过长度匹配 if (text.length() == pattern.length()) { return new SimpleDateFormat(pattern).parse(text); } } break; } } return DatePatterns.parse(text); }