转:http://www.cnblogs.com/Nbge/archive/2012/07/31/2617127.htmldate
转json有时候会乱码解决的办法
package com.techson.himsnanhwa.admin.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
public class JsonDateValueProcessor implements JsonValueProcessor {
private String format ="yyyy-MM-dd";
public Object processArrayValue(Object value, JsonConfig config) {
return process(value);
}
public Object processObjectValue(String key, Object value, JsonConfig config) {
return process(value);
}
private Object process(Object value){
if(value instanceof Date){
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.UK);
return sdf.format(value);
}
return value == null ? "" : value.toString();
}
}
Tariff bean = new Tariff();
bean.setName("Robby");
bean.setAge(1);
bean.setBorn(new Date());
JsonConfig jsonConfig = new JsonConfig(); //JsonConfig是net.sf.json.JsonConfig中的这个,为固定写法
jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());
JSONObject jo = JSONObject.fromObject(bean, jsonConfig);
System.out.println("json:" + jo.toString());
//结果:json:{"age":1,"born":"2011-05-31","name":"Robby"}