Packagejsondateprocess;Importjava.sql.Date;ImportJava.text.SimpleDateFormat;ImportJava.util.Locale;ImportNet.sf.json.JsonConfig;ImportNet.sf.json.processors.JsonValueProcessor;/*** * @description: For Jsonarray.fromobject () the date class (1) returns an empty string for a null value in the date Time property, because the problem occurs during conversion , although no exception occurred, the AJAX request reported 404 Not Found Error * (2) format of the conversion date * @fileName: Jsondatevalueprocessor.java * @createTime: 2015 December 8 a.m. 11:33:39 *@author: Liwenjian *@version1.0.0 **/ Public classJsondatevalueprocessorImplementsJsonvalueprocessor {@Override Publicobject Processarrayvalue (Object obj, Jsonconfig jsonconfig) {returnprocess (obj); } @Override PublicObject Processobjectvalue (String s, Object obj, Jsonconfig jsonconfig) {returnprocess (obj); } Privateobject Process (Object obj) {if(obj = =NULL) {//If the time is null, the empty string is returned return""; } if(objinstanceofDate) {obj=Newjava.util.Date ((Date) obj). GetTime ()); } if(objinstanceofjava.util.Date) {SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd", Locale.china);//format time is YYYY-MM-DD type returnSdf.format (obj); } Else { return NewObject (); } }}
Packagejsondateprocess;Importjava.util.Date;/*** * @description: User table * @fileName: User.java * @createTime: December 8, 2015 PM 2:55:46 *@author: Liwenjian *@version1.0.0 **/ Public classUser {PrivateString name; PrivateDate CreateDate; PrivateDate updatedate; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicDate getcreatedate () {returnCreateDate; } Public voidsetcreatedate (Date createdate) { This. CreateDate =CreateDate; } PublicDate getupdatedate () {returnupdatedate; } Public voidsetupdatedate (Date updatedate) { This. updatedate =updatedate; }}
Packagejsondateprocess;Importjava.util.ArrayList;Importjava.util.Date;Importjava.util.List;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JsonConfig;/*** * @description: Test class * @fileName: Jsondateformat.java * @createTime: December 8, 2015 PM 3:03:00 *@author: Liwenjian *@version1.0.0 **/ Public classJsondateformat { Public Static voidMain (string[] args) {List<User> list =NewArraylist<user>(); User User=NewUser (); User.setname ("Zhang San"); User.setcreatedate (NewDate ()); User.setupdatedate (NewDate ()); List.add (user); User=NewUser (); User.setname ("John Doe"); User.setcreatedate (NewDate ()); User.setupdatedate (NULL); List.add (user); Jsonconfig Jsonconfig=NewJsonconfig (); Jsonconfig.registerjsonvalueprocessor (Date.class, NewJsondatevalueprocessor ());//Injection Processing Date classJsonarray Jsonarray =jsonarray.fromobject (list, jsonconfig); System.out.println (Jsonarray.tostring ()); }}
Output: [{"CreateDate": "2015-12-08", "name": "Zhang San", "updatedate": "2015-12-08"},{"CreateDate": "2015-12-08", "name": "John Doe" , "Updatedate": ""}]
The jar packages involved are as follows:
Jsonarray.fromobject () Injection Processing date format