Json-lib-java.util.date Conversion Issues

Source: Internet
Author: User

When you use Json-lib to convert a Java.util.Date object directly into a JSON string, you usually get a format that is not what you want:

System.out.println (Jsonserializer.tojson (new  Date () )); // {"Date": +, "Day": 3, "hours": minutes, "month": 5, "Seconds": $, "time": 1435120786584, "Timezoneoffset":- 480, "Year":

JavaBean Turn JSON

Jsonvalueprocessor Jsonvalueprocessor =Newjsonvalueprocessor () {Private FinalString date_format = "Yyyy-mm-dd"; @Override PublicObject Processobjectvalue (String key, object value, Jsonconfig config) {if(Value = =NULL) {            return""; }        if(ValueinstanceofDate) {            return NewSimpleDateFormat (date_format). FORMAT ((DATE) value); }        returnvalue.tostring (); } @Override PublicObject Processarrayvalue (object value, Jsonconfig config) {return NULL; }}; Jsonconfig Jsonconfig=Newjsonconfig (); Jsonconfig.registerjsonvalueprocessor (Date.class, Jsonvalueprocessor); Student Stud=NewStudent ("1001", "Huey", ' M ',NewDate ()); String Jsonstr=Jsonserializer.tojson (Stud, jsonconfig). toString ();    System.out.println (JSONSTR); //{"Birthday": "2015-06-24", "Gender": "M", "Studname": "Huey", "Studno": "1001"}

JSON Turn JavaBean

String jsonstr = "{' Studno ': ' 1001 ', ' Studname ': ' Huey ', ' Gender ': ' M ', ' Birthday ': ' 2014-04-13 '}"=  Jsonobject.fromobject (JSONSTR); Jsonutils.getmorpherregistry (). Registermorpher (new datemorpher (new string[]{"Yyyy-mm-dd"  = (Student) Jsonobject.tobean (Jsonobj, Student.  Class); SYSTEM.OUT.PRINTLN (stud);     // Student (studno=1001, Studname=huey, gender=m, Birthday=sun Apr 00:00:00 CST)

JavaBean definition

@Data @noargsconstructor@allargsconstructor  Public class Student {    private  String studno;     Private String studname;     Private Char gender;     Private Date birthday;    }

Json-lib-java.util.date Conversion Issues

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.