创作工具类
- 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";
- private DateFormat dateFormat;
-
- public DateJsonValueProcessor(String datePattern) {
- try {
- dateFormat = new SimpleDateFormat(datePattern);
- } catch (Exception e) {
- 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) {
- return dateFormat.format((Date) value);
-
- }
- }
进行測试
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
-
- import net.sf.json.JSONObject;
- import net.sf.json.JsonConfig;
-
- public class Test {
- public static void main(String[] args) {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("time", new Date());
- map.put("name", "yy");
- map.put("age", 20);
- JsonConfig config = new JsonConfig();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("G yyyy-MM-dd hh:mm:ss.SS zzz ZZZ w DDD FF EE"));
- JSONObject Obj = JSONObject.fromObject(map, config);
-
- System.out.println(Obj);
- }
- }
原文出自:http://blog.csdn.net/heardy/article/details/6760722
版权声明:本文博主原创文章,博客,未经同意不得转载。