1、DateJsonValueProcessor.java
package com.px.util; import java.sql.Timestamp; import java.text.SimpleDateFormat; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; import java.util.Date; /** * JSONArray对日期类型的按指定格式转换的实现 * * @author liyulin lyl010991@126.com * @version 1.0 2015-03-17 */ public class DateJsonValueProcessor implements JsonValueProcessor { private String formate; public DateJsonValueProcessor() { this.formate = "yyyy-MM-dd hh:mm:ss"; } public DateJsonValueProcessor(String formate) { this.formate = formate; } public Object processArrayValue(Object value, JsonConfig jsonConfig) { return null; } public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { if (value instanceof Date || value instanceof Timestamp) { String str = new SimpleDateFormat(this.formate).format((Date) value); return str; } return null; } public String getFormate() { return formate; } public void setFormate(String formate) { this.formate = formate; } }
JsonConfig jf = new JsonConfig(); jf.registerJsonValueProcessor(java.sql.Timestamp.class, new DateJsonValueProcessor()); jf.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor()); JSONArray.fromObject(ssypList, jf).toString();
原文地址:http://blog.csdn.net/class_horse/article/details/44340513