Jsonarray.fromobject () Injection Processing date format

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.