public class Demo2 {
/**
* @param args
* @throws Exception
* @throws IllegalAccessException
*/
public static void main(String[] args) throws Exception {
Student s = new Student();
BeanUtils bu = new BeanUtils();
//向BeanUtils框架注册自己的转换器(String -> Date)
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class arg0, Object arg1) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse((String) arg1);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}}, java.util.Date.class);
bu.setProperty(s, "name", "小青");
bu.setProperty(s, "age", "20");
bu.setProperty(s, "birthday", "1992-02-05");
String nameva l = bu.getProperty(s, "name");
System.out.println("name = "+nameva l);
String ageva l = bu.getProperty(s, "age");
System.out.println("age = "+ageva l);
String bitVal = bu.getProperty(s, "birthday");
System.out.println("birthday = "+bitVal);
}
}
运行结果:
name = 小青
age = 20
birthday = Wed Feb 05 00:00:00 CST 1992