When passing data through JSON, it is often necessary to pass the date, which in Java can be converted to JSON data in a specific format by Gson.
1. Normal Gson Conversion Date
void Query (httpservletrequest request, httpservletresponse response, Registerservice Service) throws Exception { = Integer.parseint (Request.getparameter ("id")); = Service.getregisterbyid (ID); New Gson (); = Gson.tojson (register); SYSTEM.OUT.PRINTLN (JSON); Response.getwriter (). write (JSON); }
Date format after conversion:
Front Screen display:
2. If you need to convert the date format to data in a specific date format:
//Enquiry Public voidQuery (httpservletrequest request, httpservletresponse response, Registerservice service)throwsException {Integer ID= Integer.parseint (Request.getparameter ("id")); Register Register=Service.getregisterbyid (ID); //Convert the date format, or you can new Gsonbuilder (). Setdateformat ("Yyyy-mm-dd HH:mm:ss"). Create ();Gson Gson =NewGsonbuilder (). Setdateformat ("Yyyy-mm-dd"). Create (); String JSON=Gson.tojson (register); SYSTEM.OUT.PRINTLN (JSON); Response.getwriter (). write (JSON); }
Converted date JSON:
The format of the date displayed by the foreground interface:
Gson convert date data to specific JSON data