就是在拷贝的时候加个正则的校验,如果是纯数字的日期 就转成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);
}
}
}
}
}
}

}

重写org.springframework.beans.BeanUtils的copyProperties方法,能在实体映射的时候把纯数字格式的日期转格式的更多相关文章

  1. org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils的copyProperties用法区别

    知识点 org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都提供了copyProperties方法,作 ...

  2. org.springframework.beans.BeanUtils

    org.springframework.beans.BeanUtils的一个demo.可以很优雅的实现将父类字段的值copy到子类中 下面例子的输出结果(子类使用父类的toString方法,有点意思吧 ...

  3. BeanUtils的copyproPerties方法的用法

    转自:Hassan Blog的博客 一.简介:  BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean通常包 ...

  4. org.springframework.beans.BeanUtils的用法

    s,t为对象BeanUtils.copyProperties(s, t);  将给定源bean的属性值复制到目标bean中 public static void copyProperties(Obje ...

  5. BeanUtils工具类copyProperties方法缺点及解决

    使用类为spring-beans:4.3.13release包中的 org.springframework.beans.BeanUtils BeanUtils.copyProperties(Objec ...

  6. 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation

    解决方法为将selenium-server-standalone-2.37.0.jar升级至selenium-server-standalone-2.41.0.jar即可. 下载地址:http://s ...

  7. 开发Spring过程中几个常见异常(二):Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a' define

    本异常是小编在运行自己另外一篇博文中的例子时遇到的.(附博文:http://www.cnblogs.com/dudududu/p/8482487.html) 完整异常信息: 警告: Exception ...

  8. springmvc上传文件报错org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]

    在用springmvc+mybatis进行项目开发时,上传文件抛异常... org.springframework.beans.BeanInstantiationException: Could no ...

  9. 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface

    后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...

  10. org.springframework.beans.factory.BeanCreationException: 求教育!

    2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...

随机推荐

  1. anaconda在sys.path删除~/.local/lib

    python -m site python -m site -help USER_SITE='~/anaconda3/envs/test/lib/python3.7/site-packages'

  2. 【Python3+Selenium】基本操作

    一.环境准备 1.Selenium安装教程 1.1 打开cmd,输入如下命令:pip/pip3 install selenium 1.2 安装完成后查看Python路径包管理(Lib\site-pac ...

  3. windows使用问题(win10)

    常见问题 遇到修改文件夹名称被程序占用解决办法 1.ctrl+shift+esc进入任务管理器 2.打开性能-资源监视器 3.关联的句柄搜索框输入文件夹名称 4.搜查出来暂用文件夹程序选中右键将其关闭

  4. AtCoder Beginner Contest 272 - G - Yet Another mod M

    随机 + 数论 题意 Submission #35524126 - AtCoder Beginner Contest 272 给一个长度为 \(n\;(1<=n<=5000)\) 的数组 ...

  5. Word06 黑客技术office真题

    1.课程的讲解之前,先来对题目进行分析,首先需要在考生文件夹下,将Wrod素材.docx文件另存为Word.docx,后续操作均基于此文件,否则不得分. 2.这一步非常的简单,打开下载素材文件,在[文 ...

  6. mysql实训

    MYSQL You have an error in your SQL syntax; check the manual that corresponds to your MySQL server v ...

  7. mac上创建第一个C程序

    在mac电脑上,写C语言程序一般用终端来写,我们学习C主要是为了学习iOS的话,我们今天换Xcode来写C. 一.去App Store或者苹果开发者网站上下载Xcode.打开Xcode,创建项目. 二 ...

  8. 计算机网络基础(3):IP与子网掩码/ ping/ ipconfig/ VLAN/ 网络服务器配置

    chapter4 构建中型网络 1. IP地址与子网掩码 A类地址:网络ID开头是0,范围从00000001到01111110,126个,其中0 127留作他用.在每个网段里(网络ID),可以容纳2* ...

  9. Unity 简易聊天室(基于TCP)(1)

    为了准备毕业设计,学习了服务器与客户端之间传输的一些简单的知识,并跟着网络上的教程制作了一个简易的Unity聊天室 服务器:用C# .Net Framework写的 结构分为:main(主函数).Se ...

  10. 5G工业网关在智能工厂的应用案例

    智能工厂是5G技术的重要应用场景之一.利用5G网络将生产设备无缝连接,并进一步打通设计.采购.仓储.物流等环节,使生产更加扁平化.定制化.智能化,从而构造一个面向未来的智能制造网络. 5G 作为最优的 ...