// 格式化日期
JsonConfig jsonConfig = new JsonConfig();
JsonDateValueProcessor dshJsonDateValueProcessor = new JsonDateValueProcessor();
jsonConfig.registerJsonValueProcessor(Timestamp.class, dshJsonDateValueProcessor);
JSONArray jsonArray = JSONArray.fromObject(List, jsonConfig);
另建一个类
JsonDateValueProcessor
package com.jh.Admin.controller;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
public class JsonDateValueProcessor implements JsonValueProcessor {
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 Date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(value); } else if (value instanceof Timestamp) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(value); } return value == null ? "" : value.toString(); }
}
///
jsonArray转List:
JSONArray array = JSONArray.fromObject(xxxList);
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" }), true);
List appUserGroupList = (List) JSONArray.toList(array, new xxx, new JsonConfig());
版权声明:文章来源于网络采集,版权归原创者所有,均已注明来源,如未注明可能来源未知,如有侵权请联系管理员删除。