Apache的BeanUtils对日期的支持

在Java对象的拷贝过程中,Apache的BeanUtils对日期的支持不是很好,但有扩展,大家可以找到它的日期转换器,注册进去就ok了

创建BeanUtils的子类,把日期转换器注册进去,以后用这个子类来copy对象:

public class BeanUtilEx extends BeanUtils {

private static Map cache = new HashMap();
private static Log logger = LogFactory.getFactory().getInstance(BeanUtilEx.class);

private BeanUtilEx() {
}

static {
// 注册sql.date的转换器,即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.sql.Date.class);
ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.util.Date.class); 
ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlTimestampConverter(null), java.sql.Timestamp.class);
// 注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
}

public static void copyProperties(Object target, Object source)
throws InvocationTargetException, IllegalAccessException {
// 支持对日期copy
org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);

}

}

猜你喜欢

转载自love398146779.iteye.com/blog/1329399