zoukankan      html  css  js  c++  java
  • json-lib date对象转json ,加入自定义日期格式处理

    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;
        }
    }
  • 相关阅读:
    ACM Red and Black
    ACM Wooden Stricks
    ACM Strange fuction
    ACM pie
    ACM Doing Homework again
    ACM FatMouse' Trade
    ACM 今年暑假不AC
    ACM Ignatius and the Princess II
    一、数组---第三大的数
    一、数组---移动零※※※※※※
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/5066657.html
Copyright © 2011-2022 走看看