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();
             } 
         }
  • 相关阅读:
    java equals与==的区别
    java中switch能否使用String作为参数?
    java九种数据类型以及封装类
    JavaWeb-----------------Json(一)
    JavaWeb-----------Ajax页面局部刷新(三)
    Linux下的C语言读写练习(二)
    Nginx HTTP负载均衡和反向代理的配置与优化
    kafka端口和zookeeper端口
    Correct the classpath of your application so that it contains a single, compatible version of org.springframework.util.Assert
    nginx简介&nginx基本配置和优化
  • 原文地址:https://www.cnblogs.com/passer1991/p/2767235.html
Copyright © 2011-2022 走看看