日期:2014-05-16  浏览次数:20431 次

java对象转换为json字符串时对date类型处理

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>

?

JsonConfig cfg = new JsonConfig();   
           cfg.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {   
            private final String format="yyyy-MM-dd";   
            public Object processObjectValue(String key, Object value,   
              JsonConfig arg2)   
            {   
             if(value==null)   
              return "";   
             if (value instanceof Date) {   
              String str = new SimpleDateFormat(format).format((Date) value);   
              return str;   
             }   
             return value.toString();   
            }   
  
            public Object processArrayValue(Object value, JsonConfig arg1)   
            {   
             return null;   
            }   
           });   
           
        Collection<JsonBean> list = JSONArray.toCollection(JSONArray.fromObject(jsonStr,cfg ), JsonBean.class);      
           
        JSONArray json = JSONArray.fromObject(votes,cfg);  

?