zoukankan      html  css  js  c++  java
  • 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();
             } 
         }
  • 相关阅读:
    POJ 1141 Brackets Sequence (区间DP)
    UVaLive 6585 && Gym 100299F Draughts (暴力+回溯)
    UVaLive 6950 && Gym 100299K Digraphs (DFS找环或者是找最长链)
    UVaLive 6588 && Gym 100299I (贪心+构造)
    UVa 1611 Crane (构造+贪心)
    Gym 100299C && UVaLive 6582 Magical GCD (暴力+数论)
    UVa 1642 Magical GCD (暴力+数论)
    UVaLive 6591 && Gym 100299L Bus (水题)
    UVaLive 6581 && Gym 100299B What does the fox say? (模拟+STL)
    HDU 5898 odd-even number (数位DP)
  • 原文地址:https://www.cnblogs.com/passer1991/p/2767235.html
Copyright © 2011-2022 走看看