首页 > 代码库 > 使用BeanUtils设置属性转换String到Date类型
使用BeanUtils设置属性转换String到Date类型
1 使用BeanUtils进行设置属性时,对于String,int可以自动转换。比如下面的例子
先定义一个Person类
package pkg6; import java.util.Date; import java.text.SimpleDateFormat; public class Person { private String name; private String gender; private int age; private Date birthday; public Person() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "{name:"+name+" age:"+age+" birthday:"+birthday+"}"; } }
使用一个函数测试一下
package pkg6; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; public class demo1 { public static void main(String[] args) { // TODO Auto-generated method stub Person p1 = new Person(); String name = "eric"; String gender = "M"; int age = 12; String birthday = "1991-32-56"; try { BeanUtils.setProperty(p1, "name", "eric"); //BeanUtils.setProperty(p1, "birthday", birthday); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(p1); } }
它的输出是
{name:eric age:0 birthday:null}
没有问题。在web开发过程中经常会遇到网页提交的用户数据是字符串,存到数据库中的数据是日期,那么直接进行设置会出现什么情况,如下所示
在设置属性的代码后面添加
BeanUtils.setProperty(p1, "birthday", birthday);
运行结果会报错
org.apache.commons.beanutils.ConversionException: String must be in JDBC format [yyyy-MM-dd] to create a java.sql.Date
at org.apache.commons.beanutils.converters.DateTimeConverter.toDate(DateTimeConverter.java:436)
at org.apache.commons.beanutils.converters.DateTimeConverter.convertToType(DateTimeConverter.java:343)
at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:156){name:eric age:0 birthday:null}
这是因为beanutils不支持字符串转换成其他对象类型。要想转换需要注册一个转换器。
ConvertUtils.register(new Converter() { public Object convert(Class type, Object value) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return simpleDateFormat.parse(value.toString()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }, Date.class);
当遇到java.util.Date类型时,就使用这个转换器,将字符串转换成Date类型。
完整的代码如下:
package pkg6; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.Converter; public class demo1 { public static void main(String[] args) { // TODO Auto-generated method stub Person p1 = new Person(); String name = "eric"; String gender = "M"; int age = 12; String birthday = "1991-32-56"; ConvertUtils.register(new Converter() { public Object convert(Class type, Object value) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return simpleDateFormat.parse(value.toString()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }, Date.class); try { BeanUtils.setProperty(p1, "name", "eric"); BeanUtils.setProperty(p1, "birthday", birthday); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(p1); } }
运行结果如下:
{name:eric age:0 birthday:Sat Sep 25 00:00:00 CST 1993}
使用BeanUtils设置属性转换String到Date类型