BeanUtils.copyProperties支持Date类型
2017-01-05 14:29
357 查看
import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.log4j.Logger; /** * @Title 扩展BeanUtils.copyProperties支持data类型 * @Description * @author wangzs * @date 2017-1-5 */ public class BeanUtilsEx extends BeanUtils { private static Logger logger = Logger.getLogger(BeanUtilsEx.class); static { ConvertUtils.register(new DateConvert(), java.util.Date.class); ConvertUtils.register(new DateConvert(), String.class); } public static void copyProperties(Object target, Object source) { // 支持对日期copy try { org.apache.commons.beanutils.BeanUtils.copyProperties(target, source); } catch (IllegalAccessException | InvocationTargetException e) { logger.error("扩展BeanUtils.copyProperties支持data类型:" + e.getMessage()); e.printStackTrace(); } } }
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
/**
* @Title 扩展BeanUtils.copyProperties支持data类型
* @Description
* @author wangzs
* @date 2017-1-5
*/
public class DateConvert implements Converter {
@Override
public Object convert(Class class1, Object value) {
if (value == null) {
return null;
}
if (value instanceof Date) {
return value;
}
if (value instanceof Long) {
Long longValue = (Long) value;
return new Date(longValue.longValue());
}
if (value instanceof String) {
String dateStr = (String) value;
Date endTime = null;
try {
String regexp1 = "([0-9]{4})-([0-1][0-9])-([0-3][0-9])T([0-2][0-9]):([0-6][0-9]):([0-6][0-9])";
String regexp2 = "([0-9]{4})-([0-1][0-9])-([0-3][0-9])(/t)([0-2][0-9]):([0-6][0-9]):([0-6][0-9])";
String regexp3 = "([0-9]{4})-([0-1][0-9])-([0-3][0-9])";
if (dateStr.matches(regexp1)) {
dateStr = dateStr.split("T")[0] + " " + dateStr.split("T")[1];
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
endTime = sdf.parse(dateStr);
return endTime;
} else if (dateStr.matches(regexp2)) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
endTime = sdf.parse(dateStr);
return endTime;
} else if (dateStr.matches(regexp3)) {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
endTime = sdf.parse(dateStr);
return endTime;
} else {
return dateStr;
}
} catch (ParseException e) {
e.printStackTrace();
}
}
return value;
}
}
相关文章推荐
- spring的BeanUtils.copyProperties(obj1, obj2);复制对象时字段类型不一致,导致赋值不上
- BeanUtils.copyProperties 在copy Integer类型问题
- BeanUtils.copyProperties扩展--String转Date
- 有关apache包里面的BeanUtils.copyProperties 字段类型为Integer或者Double时,自动赋值为默认值的问题随笔
- No value specified for 'Date' BeanUtils.copyProperties 日期为空 转型错误
- 让BeanUtils的copyProperty方法支持String与java.util.Date的类型转换
- 使用BeanUtils.copyproperties发生 No value specified for 'Date'异常
- 关于使用BeanUtils.copyProperties拷贝时,时间类型为空时转换异常
- 改进BeanUtils支持DATE类型
- BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别
- dhl:报错:LINQ to Entities 不支持指定的类型成员“Date”
- asp.net提示使用的 SQL Server 版本不支持数据类型“date”的解决办法:
- 使用BeanUtils.copyProperties()产生的错误
- 使用SQL Sever版本不支持数据类型“date”的解决办法
- 使用的 SQL Server 版本不支持数据类型“date”的解决方法
- 使用的 SQL Server 版本不支持数据类型“date”的解决方法 [转]
- 使用BeanUtils时,源目标中Date类型值为空的解决方法
- VC20010CMFCPropertyGridProperty不支持Date类型的自动输出
- 使用的 SQL Server 版本不支持数据类型“date”的解决方法 (转)