Json-lib Date object to JSON, adding custom date format processing

Source: Internet
Author: User

ImportNet.sf.json.JSONObject; ImportNet.sf.json.JsonConfig; ImportNet.sf.json.processors.JsonValueProcessor;  Public classDatejsonvalueprocessorImplementsJsonvalueprocessor { Public Static FinalString Default_date_pattern = "Yyyy-mm-dd HH:mm:ss"; PrivateDateFormat DateFormat; /*** Construction method. *         * @paramDatepattern Date Format*/       Publicdatejsonvalueprocessor (String datepattern) {Try{DateFormat=NewSimpleDateFormat (Datepattern); } Catch(Exception ex) {DateFormat=NewSimpleDateFormat (Default_date_pattern); }       }         PublicObject Processarrayvalue (object value, Jsonconfig jsonconfig) {returnprocess (value); }         PublicObject Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {returnprocess (value); }        PrivateObject Process (object value) {if(Value = =NULL) {Value=NewDate ();//returns the current date as NULL, or returns "" to see if a        }           returnDateformat.format ((Date) value); }         Public StaticString map2jsonstr (map map) {Jsonconfig Jsonconfig=NewJsonconfig (); Datejsonvalueprocessor Beanprocessor=Newdatejsonvalueprocessor (Default_date_pattern); Jsonconfig.registerjsonvalueprocessor (Date.class, Beanprocessor); Jsonobject Jsonobject=jsonobject.fromobject (map, jsonconfig); returnjsonobject.tostring (); }           //converts an object to a JSON string, using the date format defined above@SuppressWarnings ("Unchecked")        Public Staticjsonobject obj2jsonobj (Object obj) {jsonconfig jsonconfig=NewJsonconfig (); Datejsonvalueprocessor Beanprocessor=Newdatejsonvalueprocessor (Default_date_pattern); Jsonconfig.registerjsonvalueprocessor (Date.class, Beanprocessor); Jsonobject Jsonobject=jsonobject.fromobject (obj, jsonconfig); returnJsonobject; }   }   ImportNet.sf.json.JSONObject;ImportNet.sf.json.JsonConfig;ImportNet.sf.json.processors.JsonValueProcessor; Public classDatejsonvalueprocessorImplementsJsonvalueprocessor { Public Static FinalString Default_date_pattern = "Yyyy-mm-dd HH:mm:ss"; PrivateDateFormat DateFormat; /*** Construction method. *       * @paramDatepattern Date Format*/     Publicdatejsonvalueprocessor (String datepattern) {Try{DateFormat=NewSimpleDateFormat (Datepattern); } Catch(Exception ex) {DateFormat=NewSimpleDateFormat (Default_date_pattern); }    }      PublicObject Processarrayvalue (object value, Jsonconfig jsonconfig) {returnprocess (value); }      PublicObject Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {returnprocess (value); }     PrivateObject Process (object value) {if(Value = =NULL) {Value=NewDate ();//returns the current date as NULL, or returns "" to see if a        }        returnDateformat.format ((Date) value); }      Public StaticString map2jsonstr (map map) {Jsonconfig Jsonconfig=NewJsonconfig (); Datejsonvalueprocessor Beanprocessor=Newdatejsonvalueprocessor (Default_date_pattern); Jsonconfig.registerjsonvalueprocessor (Date.class, Beanprocessor); Jsonobject Jsonobject=jsonobject.fromobject (map, jsonconfig); returnjsonobject.tostring (); }         //converts an object to a JSON string, using the date format defined above@SuppressWarnings ("Unchecked")     Public Staticjsonobject obj2jsonobj (Object obj) {jsonconfig jsonconfig=NewJsonconfig (); Datejsonvalueprocessor Beanprocessor=Newdatejsonvalueprocessor (Default_date_pattern); Jsonconfig.registerjsonvalueprocessor (Date.class, Beanprocessor); Jsonobject Jsonobject=jsonobject.fromobject (obj, jsonconfig); returnJsonobject; }}

Json-lib Date object to JSON, adding custom date format processing

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.