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
BeanUtils
methods (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 the java.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 the
ConvertUtils.register()
method.
代码如下:
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
org.apache.commons.beanutils.Converter;
public
class
CustomerDateConverter
implements
Converter {
private
final
static
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();
}
}
return
null
;
}
}
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());