passer1991

beanutils的一个应用

import java.sql.Timestamp;
 import java.util.Date;
 
 public class Person {
     private Timestamp birthday;
     private String name;
     private Date date;
     
     public Date getDate() {
         return date;
     }
     public void setDate(Date date) {
         this.date = date;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     public Timestamp getBirthday() {
         return birthday;
     }
     public void setBirthday(Timestamp birthday) {
         this.birthday = birthday;
     }
 }
 


     //将yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd类型的字符串转化为java.sql.Timestamp类型的数据; 
     @Test
      public void testname4() {
          Person p=new Person();
          Map map=new HashMap();
  //        map.put("birthday", "1991-09-12 12:12:12");
          map.put("birthday", "1991-09-12");
          map.put("name", "xiaoyu");
          
          SqlTimestampConverter dtConverter = new  SqlTimestampConverter();
          dtConverter.setPatterns(new String[]{"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"});
          
          ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
          convertUtilsBean.register(dtConverter, Timestamp.class);
          
          BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean);
          try {
              beanUtilsBean.populate(p, map);
              
              System.out.println(p.getBirthday().toLocaleString());
          } catch (Exception e) {
              e.printStackTrace();
          } 
      }
     
     //这个例子是将"yyyy-MM-dd HH:mm:ss"或"yyyy-MM-dd"类型的字符串转化为java.util.Date类型的数据
     @Test
     public void testname5() {
         Person p=new Person();
         Map map=new HashMap();
         map.put("date", "1991-09-12 12:12:12");
 //        map.put("date", "1991-09-12");
         map.put("name", "xiaoyu");
         
         SqlTimestampConverter dtConverter = new  SqlTimestampConverter();
 //        DateTimeConverter dtConverter=new DateConverter("oh my god");
         dtConverter.setPatterns(new String[]{"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"});
         
         ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
         convertUtilsBean.register(dtConverter, Date.class);
         
         BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean);
         try {
             beanUtilsBean.populate(p, map);
             
             System.out.println(p.getDate().toLocaleString()+" here");
         } catch (Exception e) {
             e.printStackTrace();
         } 
     }
     //自定义了一个类型转换器
     @Test
     public void testname51() {
         Person p=new Person();
         Map map=new HashMap();
         map.put("birthday", "1991-09-12 23:34:32");
 //        map.put("date", "1991-09-12");
         map.put("name", "xiaoyu");
         
         ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
         convertUtilsBean.register(new Converter() {
             
             @Override
             public Object convert(Class type, Object value) {
                 if(value==null||"".equals(value)) 
                     return null;
                 if(!value.getClass().equals(String.class))
                 {
                     throw new ConversionException("日期转换器只支持String类型的转换");
                 }
                 SimpleDateFormat sdf=null;
                 String source=(String) value;
                 if(source.length()>10)
                     sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                 else
                     sdf=new SimpleDateFormat("yyyy-MM-dd");
                 
                 try {
                     return  new Timestamp(sdf.parse(source).getTime());
                 } catch (ParseException e) {
                     throw new RuntimeException(e);
                 }
             }
         }, Timestamp.class);
         
         BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean);
         try {
             beanUtilsBean.populate(p, map);
             
             System.out.println(p.getBirthday().toLocaleString()+" here");
         } catch (Exception e) {
             e.printStackTrace();
         } 
     }

posted on 2012-11-12 23:09  passer1991  阅读(283)  评论(0编辑  收藏  举报

导航