javaBean中 字符串 转 date 类型转换

1-----创建javabean 代码如下

 1 package BeanUtils;
 2 import java.util.Date;
 3 public class Admin {
 4     private String userName;
 5     private String age;
 6     private Date birth;
 7     public String getUserName() {
 8         return userName;
 9     }
10     public void setUserName(String userName) {
11         this.userName = userName;
12     }
13     public String getAge() {
14         return age;
15     }
16     public void setAge(String age) {
17         this.age = age;
18     }
19     
20     public Date getBirth() {
21         return birth;
22     }
23     public void setBirth(Date birth) {
24         this.birth = birth;
25     }
26     @Override
27     public String toString() {
28         return "Admin [userName=" + userName + ", age=" + age + ", birth=" + birth + "]";
29     }
30 }

2------java代码测试

 1 @Test
 2     public void test2(){
 3         String userName = "Jack";
 4         String age = "25";
 5         String birth = "1995-08-08";
 6         Admin admin = new Admin();
 7         //注册日期类型转换
 8         ConvertUtils.register(new Converter() {
 9             @Override
10             public Object convert(Class type, Object value) {
11                 //判断
12                 if(type != Date.class){
13                     return null;
14                 }
15                 if(value == null || "".equals(value.toString().trim())){
16                     return null;
17                 }
18                 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
19                 try {
20                     return dateFormat.parse(value.toString());
21                 } catch (Exception e) {
22                     e.printStackTrace();
23                     throw new RuntimeException(e);
24                 }
25             }
26         }, Date.class);
27         // 把表单提交的数据,封装到对象中
28         try {
29             BeanUtils.copyProperty(admin, "userName", userName);
30             BeanUtils.copyProperty(admin, "age", age);
31             BeanUtils.copyProperty(admin, "birth", birth);
32         } catch (Exception e) {
33             // TODO Auto-generated catch block
34             e.printStackTrace();
35             throw new RuntimeException(e);
36         } 
37         System.err.println(admin);
38     }
39     
40     //2. 使用提供的日期类型转换器工具类
41         @Test
42         public void test3() throws Exception {
43             // 模拟表单数据
44             String name = "jack";
45             String age = "20";
46             String birth = "1998-08-25";
47             // 对象
48             Admin admin = new Admin();
49             // 注册日期类型转换器:2, 使用组件提供的转换器工具类
50             ConvertUtils.register(new DateLocaleConverter(), Date.class);
51             // 把表单提交的数据,封装到对象中
52             BeanUtils.copyProperty(admin, "userName", name);
53             BeanUtils.copyProperty(admin, "age", age);
54             BeanUtils.copyProperty(admin, "birth", birth);
55             //------ 测试------
56             System.out.println(admin);
57         }

--------测试结果

原文地址:https://www.cnblogs.com/fujilong/p/5562480.html