BeanUtils与PropertyUtils区别

BeanUtils PropertyUtils 区别以及 java.util.Date 发生异常问题:
BeanUtils 外还有一个名为 PropertyUtils 的工具类,它也提供 copyProperties() 方法,作用与 BeanUtils 的同名方法十分相似,主要的区别在于后者提供类型转换功能,即发现两个 JavaBean 的同名属性为不同类型时,在支持的数据类型范围内进行转换,而前者不支持这个功能,但是速度会更快一些。 BeanUtils 支持的转换类型如下:

    * java.lang.BigDecimal
    * java.lang.BigInteger
    * boolean and java.lang.Boolean
    * byte and java.lang.Byte
    * char and java.lang.Character
    * java.lang.Class
    * double and java.lang.Double
    * float and java.lang.Float
    * int and java.lang.Integer
    * long and java.lang.Long
    * short and java.lang.Short
    * java.lang.String
    * java.sql.Date
    * java.sql.Time
    * java.sql.Timestamp


这里要注意一点, java.util.Date 是不被支持的,而它的子类 java.sql.Date 是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用 java.sql.Date 类型。否则在转换时会提示 argument mistype 异常。
解决方法一:如果变为 java.sql.Date 这样用 BeanUtils 用可以和 String 进行 copy property .
Address1 addr3= new  Address1();
       System. out .println(StringUtils.center( "test" , 56, "-" ));
       addr2.setDate(Date.valueOf( "2007-5-9" ));
//     PropertyUtils.copyProperties(addr2, addr3);
       BeanUtils.copyProperties(addr3, addr2);
       System. out .println(addr3.getDate());
解决方法二:自定义Converter的方法:Defining Your Own Converters
The  ConvertUtils class supports the ability to define and register your own String --> Object conversions for any given Java class. Once registered, such converters will be used transparently by all of the  BeanUtilsmethods (including  populate()). To create and register your own converter, follow these steps:
  • Write a class that implements the Converter interface. The convert() method should accept thejava.lang.Class object of your application class (i.e. the class that you want to convert to, and a String representing the incoming value to be converted.
  • At application startup time, register an instance of your converter class by calling theConvertUtils.register() method.
代码如下:
import  java.text.ParseException;
import  java.text.SimpleDateFormat;
import  org.apache.commons.beanutils.Converter;
 
 
publicclass  CustomerDateConverter  implements  Converter {
     privatefinalstatic  SimpleDateFormat  DATE_FORMATE_SHOW  =  new  SimpleDateFormat( "yyyy-MM-dd" );
     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();
              }
       }
        returnnull ;
    }
}
 
public java.lang.Object convert(java.lang.Class type,
                                java.lang.Object value)
Convert the specified input object into an output object of the specified type.
Parameters:
type  - Data type to which this value should be converted ( 要转换的类型)
value  - The input value to be converted   ( 输入的值)
Returns:
The converted value
 
测试代码 :
       System. out .println(StringUtils.center( "test" , 56, "-" ));
Address1 addr1= new  Address1(); //Address1 中的 date String
Address2 addr2= new  Address2(); //Address1 中的 date java.util.Date
 
        Addr1.setDate( "2007-2-6" );
        
        CustomerDateConverter dateConverter =  new  CustomerDateConverter (); 
        ConvertUtils.register(dateConverter,Date. class );  
// 上面两句是关键!
      
        BeanUtils.copyProperties(addr2, addr1);
        System. out .println(addr2.getDate().toLocaleString());  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值