https://www.cnblogs.com/qqzy168/p/3938942.html
package com.my.json.helper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
* 将Bean中的Timestamp、Date转换为json中的日期字符串
* @author zhangyi
*
*/
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);
}
}
package com.my.json.helper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
* 将Bean中的Timestamp、Date转换为json中的日期字符串
* @author zhangyi
*
*/
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);
}
}
package com.my.json.helper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
* 将Bean中的Timestamp、Date转换为json中的日期字符串
* @author zhangyi
*
*/
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);
}
}
package com.my.json.helper;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
* 将Bean中的Timestamp、Date转换为json中的日期字符串
* @author zhangyi
*
*/
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);
}
}
package com.my.json.helper;
import java.sql.Timestamp;
public class JsonTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(123456);
s.setName("李四");
s.setAge(20);
Timestamp b = Timestamp.valueOf("1992-10-19 23:52:18");//设置Timestamp、Date类型的值
s.setBirthday(b);
//含有Timestamp、Date的bean转化为Json
String jsonStr = Util.beanToJson(s,"yyyy-MM-dd HH:mm:ss");
System.out.println(jsonStr);
//json转为bean(含有Timestamp、Date)
Student s1 = (Student)Util.jsonToBean(jsonStr,Student.class);
System.out.println(s1.getBirthday().toLocaleString());
}
}