自定义Converter的方法:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.beanutils.Converter;
publicclass CustomerDateConverter implements Converter {
private final static SimpleDateFormat DATE_FORMATE_SHOW = new SimpleDateFormat("yyyyMMddHHmmss");//根据传来的时间字符串格式:例如:20130224201210
public Object convert(Class type, Object value){
// TODO Auto-generated method stub
if (type.equals(java.util.Date.class) ) {
try {
return DATE_FORMATE_SHOW.parse(value.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
测试代码:
Address1 addr1=new Address1(); //Address1中的date是String
Address2 addr2=new Address2(); //Address1中的date是java.util.Date
Addr1.setDate("20130224201210");
//进行日期格式转换
CustomerDateConverter dateConverter = new CustomerDateConverter ();
ConvertUtils.register(dateConverter,Date.class);
//上面两句是关键!
BeanUtils.copyProperties(addr2, addr1);//进行复制