• BeanUtils处理日期问题
  • 先建一个Converter对象   
  • import org.apache.commons.beanutils.Converter;   
  •   
  • public class DateConvert implements Converter{   
  •   
  •     public Object convert(Class arg0, Object arg1) {   
  •         String p = (String)arg1;   
  •         if(p== null || p.trim().length()==0){   
  •             return null;   
  •         }   
  •         try{   
  •             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
  •             return df.parse(p.trim());   
  •         }   
  •         catch(Exception e){   
  •             return null;   
  •         }   
  •     }   
  •   
  • }   
  • 在使用之前先注册   
  • import org.apache.commons.beanutils.ConvertUtils;   
  •   
  • ConvertUtils.register(new DateConvert(), java.util.Date.class);   
  •   
  • 这样再用BeanUtils就没问题了   

    -----------



        //因为要注册converter,所以不能再使用BeanUtils的静态方法了,必须创建BeanUtilsBean实例
        BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());
       beanUtils.setProperty(bean, name, value);

  • 评论
    发表评论

    您还没有登录,请登录后发表评论

    july_typhoon
    搜索本博客
    博客分类
    存档
    最新评论