ConvertUtils.register(new DateLocaleConverter(),java.util.Date.class);
(6)要使用的两个jar包:
commons-beanutils-1.8.0.jar和commons-logging.jar
将这两个包复制到MyEclipse或者Eclipse中后,鼠标放在jar包上,右键选择“Build Path”-->"Add to Build Path"即可在代码中用这两个jar包了(并且这样代码中才有提示)
2.代码练习:
Student的代码(Student.java):
package cn.wwh.www.java.beanutils;
import java.util.Date;
/**
*类的作用:特别注意:开始将birthday写出birthDay,而在beanutils中仍然用的是birthday,此时程序不能通过,
*getBirthday和setBirthday中的D改过来后,才运行成功,这完全符合之前的说的,框架和setXxx和getXxx有关而与private Date birthay;中的birthDay无关
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-7-21 上午11:49:19
*/
public class Student {
private String name;
private int age;
private Date birthday;
public Student(){
System.out.println("构造函数");
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the birthDay
*/
public Date getBirthday() {
return birthday;
}
/**
* @param birthDay the birthDay to set
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
BeanUtils的测试代码(Demo1.java):
package cn.wwh.www.java.beanutils;
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;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
/**
*类的作用:因为BeanUtil只能转换八种基本类型,如果转换其他类型,
*则需要自己定义转换方式, 例如:想转换日期类型
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-7-21 上午11:49:07
*/
public class Demo {
// 自定一个类型转化器(将String---->Date格式化的输出)
@Test
public void testDemo1() throws Exception {
Student student = new Student();
BeanUtils bu = new BeanUtils();
//一定要先注册下
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class calzz, Object type) {
/**
* 参数1:class,java.uitl.Date;(目标类型) 参数2:传入参数的类型,java.lang.string;
*/
String strBirthday = (String) type;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(strBirthday);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}, Date.class);
bu.setProperty(student, "name", "一叶扁舟");
bu.setProperty(student, "age", "22");
bu.setProperty(student, "birthday", "2014-07-22");
// 取出数据
String name = bu.getProperty(student, "name");
String age = bu.getProperty(student, "age");
String birthday = bu.getProperty(student, "birthday");
System.out.println("name:" + name + "\nage:" + age + "\nbirthday:"
+ new Date(birthday).toLocaleString());
/*
* 输出结果: 构造函数
* name:一叶扁舟
* age:22
* birthday:2014-7-22 14:00:00(我并没有修改时间,与里面源代码调用有关)
*/
}
@Test
public void testDemo2() throws Exception{
Student student = new Student();
BeanUtils bu = new BeanUtils();
ConvertUtils.register(new DateLocaleConverter(), java.util.Date.class);
bu.setProperty(student, "name", "一叶扁舟");
bu.setProperty(student, "age", "22");
bu.setProperty(student, "birthday", "2014-07-22");
// 取出数据
String name = bu.getProperty(student, "name");
String age = bu.getProperty(student, "age");
String birthday = bu.getProperty(student, "birthday");
System.out.println("name:" + name + "\nage:" + age + "\nbirthday:"
+ new Date(birthday).toLocaleString());
}
}
代码测试效果图:
原文地址:http://blog.csdn.net/u011662320/article/details/38031935