Problem Description:
This is how the time type in Java is converted into JSON data:
"Createtime": {"date": +, "Day": 3, "hours": +, "minutes": +, "month": 3, "Nanos": 0, "seconds"
: "Time": 1209539678000, "Timezoneoffset": -480, "Year": 108}
The desired result:
Convert Date to YYYY-MM-DD form
Solution:
To register the Time field processor, use Jsonconfig to:
Jsonconfig jsonconfig = new Jsonconfig ();
Jsonconfig.registerjsonvalueprocessor (Java.util.Date.class, New Datejsonvalueprocessor ("Yyyy-mm-dd HH:mm:ss"));
Jsonobject jsonobj = jsonobject.fromobject (map, jsonconfig);
Class Datejsonvalueprocessor implements Jsonvalueprocessor {
Private String format = "YYYY-MM-DD";
Public Datejsonvalueprocessor () {
}
Public datejsonvalueprocessor (String format) {
This.format = format;
}
public object Processarrayvalue (object value, Jsonconfig jsonconfig) {
string[] obj = {};
if (value instanceof date[]) {
SimpleDateFormat SF = new SimpleDateFormat (format);
date[] dates = (date[]) value;
obj = new String[dates.length];
for (int i = 0; i < dates.length; i++) {
Obj[i] = Sf.format (Dates[i]);
}
}
return obj;
}
Public Object Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {
if (value instanceof Date) {
String str = new SimpleDateFormat (format). Format ((Date) value);
return str;
}
return value.tostring ();
}
Public String GetFormat () {
return format;
}
public void SetFormat (String format) {
This.format = format;
}
The above originates from the network
Java Time type conversion jsonvalueprocessor