- public class Person {
- private String name;
- private int age;
- private String address;
- private Date birthday;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- }
复制代码
这是Person类
- public class Test1 {
- String name="cang";
- String age="23";
- String birthday="1992-10-03";
- String address="BeiJing";
- Person p=new Person();
- @Test
- public void test1() throws IllegalAccessException, InvocationTargetException
- {
- ConvertUtils.register(new Converter() {
-
- @Override
- public Object convert(Class type, Object value) {
- if(value==null)
- return null;
- if(!(value instanceof String))
- throw new ConversionException("只支持String类型!!!");
- if(((String) value).trim().equals(""))
- return null;
- SimpleDateFormat dateFormat=new SimpleDateFormat();
- Date birth;
- try {
- birth = dateFormat.parse((String) value);
- } catch (ParseException e) {
- throw new RuntimeException(e);
- }
- return birth;
- }
- }, Date.class);
- String line_separator=System.getProperty("line_separator");
- BeanUtils.setProperty(p, "name", name);
- BeanUtils.setProperty(p, "age", age);//只是支持8种基本类型之间的转换
- BeanUtils.setProperty(p, "birthday", birthday);//如果想让string型转成Date需要给beanUtils一个转换器
- BeanUtils.setProperty(p, "address", address);
-
- System.out.println(p.getName()+line_separator+p.getAge()+line_separator+p.getBirthday()+line_separator+p.getAddress()
- );
- }
- }
复制代码
运行就报了我帖子题目的错误:
错误的提示是:
java.lang.RuntimeException: java.text.ParseException: Unparseable date: "1992-10-03"
at chj.beanutils.Test1$1.convert(Test1.java:67)
at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:428)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1002)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:313)
at chj.beanutils.Test1.test1(Test1.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.text.ParseException: Unparseable date: "1992-10-03"
at java.text.DateFormat.parse(DateFormat.java:337)
at chj.beanutils.Test1$1.convert(Test1.java:65)
... 28 more
|
|