日期:2014-05-16 浏览次数:20480 次
{"date":6,"day":4,"hours":16,"minutes":46,"month":4,"nanos":0,"seconds":35,"time":1273135595000,"timezoneOffset":-480,"year":110}
JsonConfig cfg = new JsonConfig(); cfg.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor());
JSONArray array = JSONArray.fromObject(list, cfg);这样就实现了控制时间格式的需求了。
package com.scsk.common; import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; 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; public DateJsonValueProcessor() { this(DEFAULT_DATE_PATTERN); } 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 instanceof Timestamp) return dateFormat.format((Timestamp) value); else if(value instanceof Date) return dateFormat.format((Date) value); else if(value==null) return ""; else return value.toString(); } }