码迷,mamicode.com
首页 > Web开发 > 详细

json-lib date对象转json ,加入自定义日期格式处理

时间:2015-12-22 16:10:42      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

import net.sf.json.JSONObject;   
import net.sf.json.JsonConfig;   
import net.sf.json.processors.JsonValueProcessor;   
 
public class DateJsonValueProcessor implements JsonValueProcessor {   
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";   
    private DateFormat dateFormat;   
 
    /**    
     * 构造方法.    
     *    
     * @param datePattern 日期格式    
     */  
    public DateJsonValueProcessor(String datePattern) {   
        try {   
            dateFormat = new SimpleDateFormat(datePattern);   
        } catch (Exception ex) {   
            dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);   
        }   
    }   
 
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {   
        return process(value);   
    }   
 
    public Object processObjectValue(String key, Object value,   
            JsonConfig jsonConfig) {   
        return process(value);   
    }   
 
    private Object process(Object value) {   
        if (value == null) {   
            value = new Date();   //为null时返回当前日期,也可以返回"",看需要   
        }   
        return dateFormat.format((Date) value);   
    }   
 
    public static String map2JSonStr(Map map) {   
        JsonConfig jsonConfig = new JsonConfig();   
        DateJsonValueProcessor beanProcessor = new DateJsonValueProcessor(   
                DEFAULT_DATE_PATTERN);   
        jsonConfig.registerJsonValueProcessor(Date.class, beanProcessor);   
 
        JSONObject jsonObject = JSONObject.fromObject(map, jsonConfig);   
 
        return jsonObject.toString();   
    }      
 
    //将对象转换为json string,使用上面定义的的日期格式   
    @SuppressWarnings("unchecked")   
    public static JSONObject obj2JsonObj(Object obj) {   
        JsonConfig jsonConfig = new JsonConfig();   
        DateJsonValueProcessor beanProcessor = new DateJsonValueProcessor(   
                DEFAULT_DATE_PATTERN);   
        jsonConfig.registerJsonValueProcessor(Date.class, beanProcessor);   
 
        JSONObject jsonObject = JSONObject.fromObject(obj, jsonConfig);   
 
        return jsonObject;   
    }   
}  
 
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
 
public class DateJsonValueProcessor implements JsonValueProcessor {
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
    private DateFormat dateFormat;
 
    /**  
     * 构造方法.  
     *  
     * @param datePattern 日期格式  
     */
    public DateJsonValueProcessor(String datePattern) {
        try {
            dateFormat = new SimpleDateFormat(datePattern);
        } catch (Exception ex) {
            dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        }
    }
 
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        return process(value);
    }
 
    public Object processObjectValue(String key, Object value,
            JsonConfig jsonConfig) {
        return process(value);
    }
 
    private Object process(Object value) {
        if (value == null) {
            value = new Date();   //为null时返回当前日期,也可以返回"",看需要
        }
        return dateFormat.format((Date) value);
    }
 
    public static String map2JSonStr(Map map) {
        JsonConfig jsonConfig = new JsonConfig();
        DateJsonValueProcessor beanProcessor = new DateJsonValueProcessor(
                DEFAULT_DATE_PATTERN);
        jsonConfig.registerJsonValueProcessor(Date.class, beanProcessor);
 
        JSONObject jsonObject = JSONObject.fromObject(map, jsonConfig);
 
        return jsonObject.toString();
    }    
 
    //将对象转换为json string,使用上面定义的的日期格式
    @SuppressWarnings("unchecked")
    public static JSONObject obj2JsonObj(Object obj) {
        JsonConfig jsonConfig = new JsonConfig();
        DateJsonValueProcessor beanProcessor = new DateJsonValueProcessor(
                DEFAULT_DATE_PATTERN);
        jsonConfig.registerJsonValueProcessor(Date.class, beanProcessor);
 
        JSONObject jsonObject = JSONObject.fromObject(obj, jsonConfig);
 
        return jsonObject;
    }
}

 

json-lib date对象转json ,加入自定义日期格式处理

标签:

原文地址:http://www.cnblogs.com/jinzhiming/p/5066657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!