BeanUtils的日期问题

//注册日期类型转换器  

//第一种  自定义方法
            ConvertUtils.register(new Converter(){
                //第一个参数是目标类型    第二个参数是被转换的值
                //就是 把第二个参数的值转换成第一个参数类型的值 value----》clazz类型的值
                public Object convert(Class clazz, Object value) {
                    //字符串转换为日期

        //判断传入的值
                    if(clazz !=Date.class){
                        return null;
                    }
                    if("".equals(value.toString().trim())||value==null){
                        return null;
                    }
                    try {
                        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                        return sdf.parse(value.toString());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
                
            },Date.class);

//第二种方式

//有个弊端   不能传入""(空字符串)或者"   "(多个空格)。会报错。
ConvertUtils.register(new DateLocaleConverter(), Date.class);

posted @ 2016-04-21 11:16  李永  阅读(499)  评论(0编辑  收藏  举报