Json serialization solves the Date problem

Source: Internet
Author: User

List <SoftwareDownloadCount> softWares = softWareService. softWareList (user );
JSONObject json = new JSONObject ();
 
JsonConfig config = new JsonConfig ();
Config. setIgnoreDefaultExcludes (false );
Config. setCycleDetectionStrategy (CycleDetectionStrategy. LENIENT );
Config. registerJsonValueProcessor (Date. class, new JsonDateValueProcessor (JsonDateValueProcessor. TIME_FORMAT); // date processor register
JSONArray joson = JSONArray. fromObject (softWares, config );

Return ajaxJson (joson. toString ());

 


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


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


/**
* JSON date format processing (converting java to JSON)
* @ Author sailor
*/
Public class JsonDateValueProcessor implements JsonValueProcessor {
 
/**
* DatePattern
*/
Private String datePattern = "yyyy-MM-dd ";
// Long Date Format
Public static String TIME_FORMAT = "yyyy-MM-dd HH: mm: ss ";
// The long date format is accurate to milliseconds.
Public static String LONG_TIME_FORMAT = "yyyy-MM-dd HH: mm: ss SSS ";
 
/**
* JsonDateValueProcessor
*/
Public JsonDateValueProcessor (){
Super ();
}
 
/**
* @ Param format
*/
Public JsonDateValueProcessor (String format ){
Super ();
This. datePattern = format;
}
 
/**
* @ Param value
* @ Param jsonConfig
* @ Return Object
*/
Public Object processArrayValue (Object value, JsonConfig jsonConfig ){
Return process (value );
}
 
/**
* @ Param key
* @ Param value
* @ Param jsonConfig
* @ Return Object
*/
Public Object processObjectValue (String key, Object value,
JsonConfig jsonConfig ){
Return process (value );
}
 
/**
* Process
* @ Param value
* @ Return
*/
Private Object process (Object value ){
Try {
If (value instanceof Date ){
SimpleDateFormat sdf = new SimpleDateFormat (datePattern,
Locale. UK );
Return sdf. format (Date) value );
}
Return value = null? "": Value. toString ();
} Catch (Exception e ){
Return "";
}
 
}
 
/**
* @ Return the datePattern
*/
Public String getDatePattern (){
Return datePattern;
}
 
/**
* @ Param pDatePattern the datePattern to set
*/
Public void setDatePattern (String pDatePattern ){
DatePattern = pDatePattern;
}
 
}


 

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.