BeanUtils进行日期格式的拷贝转换

来源:互联网 发布:阿里巴巴阿里云事业部 编辑:程序博客网 时间:2024/05/20 19:32
自定义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是StringAddress2 addr2=new Address2(); //Address1中的date是java.util.DateAddr1.setDate("20130224201210");//进行日期格式转换 CustomerDateConverter dateConverter = new CustomerDateConverter ();  ConvertUtils.register(dateConverter,Date.class);//上面两句是关键!BeanUtils.copyProperties(addr2, addr1);//进行复制 

  

阅读全文
0 0
原创粉丝点击