Json中的Date型別和Float型別的轉換
阿新 • • 發佈:2019-01-30
第一種方式:寫一個工具類:
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 HH:mm";
@Override
public Object processArrayValue(Object value, JsonConfig config) {
return process(value);
}
@Override
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();
}
}
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 HH:mm";
@Override
public Object processArrayValue(Object value, JsonConfig config) {
return process(value);
}
@Override
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();
}
}