package com.yihu.utils; import java.text.SimpleDateFormat; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; public class DateJsonValueProcessor implements JsonValueProcessor { private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); private SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public DateJsonValueProcessor() { } public Object processArrayValue(Object value, JsonConfig jsonConfig) { String[] obj = {}; if (value instanceof java.sql.Timestamp[]) { java.sql.Timestamp[] dates = (java.sql.Timestamp[]) value; obj = new String[dates.length]; for (int i = 0; i < dates.length; i++) { obj[i] = sf2.format(new java.util.Date(dates[i].getTime())); } }else if (value instanceof java.util.Date[]) { java.util.Date[] dates = (java.util.Date[]) value; obj = new String[dates.length]; for (int i = 0; i < dates.length; i++) { obj[i] = sf.format(dates[i]); } }else if (value instanceof java.sql.Date[]) { java.sql.Date[] dates = (java.sql.Date[]) value; obj = new String[dates.length]; for (int i = 0; i < dates.length; i++) { obj[i] = sf.format(new java.util.Date(dates[i].getTime())); } }else if (value instanceof Integer[]) { Integer[] vals = (Integer[]) value; obj = new String[vals.length]; for (int i = 0; i < vals.length; i++) { obj[i] =(vals[i]==null?null:vals[i]+"") ; } }else if (value instanceof Double[]) { Double[] vals = (Double[]) value; obj = new String[vals.length]; for (int i = 0; i < vals.length; i++) { obj[i] =(vals[i]==null?null:vals[i]+"") ; } } return obj; } public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { if (value instanceof java.sql.Timestamp) { String str =sf2.format(new java.util.Date(((java.sql.Timestamp) value).getTime())); return str; }else if (value instanceof java.util.Date) { String str = sf.format((java.util.Date) value); return str; }else if (value instanceof java.sql.Date) { String str =sf.format(new java.util.Date(((java.sql.Date) value).getTime())); return str; }else if(value!=null){ return value.toString(); }else return ""; } }