Json conversion date format

Source: Internet
Author: User
Json data conversion interface JsonValueProcessor needs to be implemented when converting the date format. [java] packageorg. rps. util; importjava. text. simpleDateFormat; importjava. util. date; importnet. sf. json. jsonConfig; importnet

Json data conversion interface JsonValueProcessor needs to be implemented when converting the date format.


[Java]
Package org. rps. util;
 
Import java. text. SimpleDateFormat;
Import java. util. Date;
 
Import net. sf. json. JsonConfig;
Import net. sf. json. processors. JsonValueProcessor;
 
/***
* Json formatted data Implementation
* @ Author YHC
*/
Public class JsonValueFormat implements JsonValueProcessor {
 
// Date Format
Private String pattern = "yyyy-MM-dd HH: mm: ss ";


@ Override
Public Object processArrayValue (Object object, JsonConfig config ){

Return null;
}
 
@ Override
Public Object processObjectValue (String key, Object value, JsonConfig jsonConfig ){
If (value instanceof Date ){
String format = new SimpleDateFormat (pattern). format (value );
Return format;
}
Return null;
}
 
}

Package org. rps. util;

Import java. text. SimpleDateFormat;
Import java. util. Date;

Import net. sf. json. JsonConfig;
Import net. sf. json. processors. JsonValueProcessor;

/***
* Json formatted data Implementation
* @ Author YHC
*/
Public class JsonValueFormat implements JsonValueProcessor {

// Date Format
Private String pattern = "yyyy-MM-dd HH: mm: ss ";
 
 
@ Override
Public Object processArrayValue (Object object, JsonConfig config ){

Return null;
}

@ Override
Public Object processObjectValue (String key, Object value, JsonConfig jsonConfig ){
If (value instanceof Date ){
String format = new SimpleDateFormat (pattern). format (value );
Return format;
}
Return null;
}

}
How to use:

1. Create a JsonConfig object.

2. register the Json value converter. The first parameter is the type to be converted, and the second parameter is the conversion processing class.

3. Add the jsonConfig object to the object for json data conversion.


[Java]
JsonConfig config = new JsonConfig ();
Config. registerJsonValueProcessor (Date. class, new JsonValueFormat ());
JSONArray jsonArray = JSONArray. fromObject (lists, config );

JsonConfig config = new JsonConfig ();
Config. registerJsonValueProcessor (Date. class, new JsonValueFormat ());
JSONArray jsonArray = JSONArray. fromObject (lists, config );


 

Related Article

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.