Time display problem when using Jsonarray.fromobject when Java.util.Date is included in object

Source: Internet
Author: User

During the two days of the project, you need to convert an object into Jsonarray in action, and I'm using Jsonarray.fromobject. However, other string types of data are already available on the page side, which is a problem with the date type. With Firebug, the date is not exactly what you want it to be (originally wanted to put together the year, month and day display). Later, the Internet to find a lot of information, finally found a way. The Code in action is as follows:

        Jsonconfig jsonconfig = new Jsonconfig ();        Jsonconfig.registerjsonvalueprocessor (Date.class, New Jsondatevalueprocessor ());        Jsonarray = Jsonarray.fromobject (user,jsonconfig);//user for my object


Jsondatevalueprocessor is a Java class that handles dates:

public class Jsondatevalueprocessor implements Jsonvalueprocessor {         private String format = "YYYY-MM-DD";              public object Processarrayvalue (object value, Jsonconfig config) {           return process (value);       }         Public Object Processobjectvalue (String key, object value, Jsonconfig config) {           return process (value);       }              Private object Process (object value) {                      if (value instanceof Date) {               SimpleDateFormat sdf = new SimpleDateFormat ( format,locale.uk);               return Sdf.format (value);           }           return value = = null? "": value.tostring ();       }   }


Note: I'm here to turn the date into YYYY-MM-DD format.

JS in the code to get the properties of the object:

var RMC = data[0].birthday;//Birthday

Note: Refer to document Http://www.cnblogs.com/Nbge/archive/2012/07/31/2617127.html


Time display problem when using Jsonarray.fromobject when Java.util.Date is included in object

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.