就是在拷贝的时候加个正则的校验,如果是纯数字的日期 就转成yyyy-MM-dd HH:mm:ss的格式
原本想直接用注解在实体转格式,但是那样实体会变成日期格式,所以放弃了,直接重写拷贝的方法比较简单

public class BeanUtilsEx extends BeanUtils {
/**
* 从org.springframework.beans.BeanUtils类中直接复制过来
* @param source
* @param target
* @throws BeansException
*/
public static void copyProperties(Object source, Object target) throws BeansException {
copyProperties(source, target, null, (String[]) null);
}

/**
* 重写方法,如果传入参数是纯数字的日期,转换后赋值
* @param source
* @param target
* @throws BeansException
*/
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties)
throws BeansException {

Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
//年月日时分秒
String regexp1 = "^([0-9]{4})(0?[1-9]|1[0-2])((0?[1-9])|((1|2)[0-9])|30|31)([0-2][0-9])([0-6][0-9])([0-6][0-9])$";
//年月日
String regexp2 = "^([0-9]{4})(0?[1-9]|1[0-2])((0?[1-9])|((1|2)[0-9])|30|31)$";
//年月
String regexp3 = "^([0-9]{4})(0?[1-9]|1[0-2])$";

Class<?> actualEditable = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
//将纯数字时间格式转换为标准格式
if (value != null) {
String dateStr = (String) value;
if (dateStr.matches(regexp1)) {
Date date = DateUtils.getDate(dateStr, "yyyyMMddHHmmss");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM-dd HH:mm:ss");
value = dateFormat;
}
if (dateStr.matches(regexp2)) {
Date date = DateUtils.getDate(dateStr, "yyyyMMdd");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM-dd");
value = dateFormat;
}
if (dateStr.matches(regexp3)) {
Date date = DateUtils.getDate(dateStr, "yyyyMM");
String dateFormat = DateUtils.formatDate(date, "yyyy-MM");
value = dateFormat;
}
}
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}

}

最新文章

  1. 社区O2O的发展与未来
  2. js无法对远程图片进行Base64转码
  3. java8 引进lamda
  4. Oracle 自己主动诊断资料档案库 (ADR)、自己主动诊断工作流、ADRCI工具
  5. 网易新闻RSS阅读器
  6. 工欲善其事必先利其器---SQL在线可视化模型设计,(还可学习拖拽知识)
  7. (白书训练计划)UVa 120 Stacks of Flapjacks(构造法)
  8. TNS-12535/12606 and ORA-3136 on Connection to Database (Doc ID 2313573.1)
  9. Java 中的 String 真的是不可变吗?
  10. 图论分支-差分约束-SPFA系统
  11. epoll(二)
  12. bzoj千题计划249:bzoj5100: [POI2018]Plan metra
  13. MS SQL动态创建临时表
  14. Apache HttpComponents 获取inputStream
  15. 【leetcode】54.Spiral Matrix
  16. C# lamda表达式
  17. ios8设置application badge value
  18. 在Window工作区按下鼠标左键拖动窗体
  19. Android无线测试之—UiAutomator UiDevice API介绍一
  20. 机器学习5—logistic回归学习笔记

热门文章

  1. What is REST and Restful?
  2. mariadb数据库用户管理(创建、赋权、)
  3. el-scrollbar 饿了么滚动条不出现 bug
  4. python中的import、from import以及import as的区别
  5. laravel 软删除的使用
  6. JS中split、slice、splice区别
  7. rabbitmq 使用管理页面向队列中推送消息
  8. JS篇(007)-事件委托是什么
  9. 通过Jsoup,爬取车辆品牌,车系,LOGO等
  10. python菜鸟学习: 7. 购物车升级版,用户、商品信息存储,修改,新增