Java Time type conversion jsonvalueprocessor

Source: Internet
Author: User
Tags string format

Problem Description:

This is how the time type in Java is converted into JSON data:
"Createtime": {"date": +, "Day": 3, "hours": +, "minutes": +, "month": 3, "Nanos": 0, "seconds"
: "Time": 1209539678000, "Timezoneoffset": -480, "Year": 108}
The desired result:

Convert Date to YYYY-MM-DD form

Solution:

To register the Time field processor, use Jsonconfig to:
Jsonconfig jsonconfig = new Jsonconfig ();
Jsonconfig.registerjsonvalueprocessor (Java.util.Date.class, New Datejsonvalueprocessor ("Yyyy-mm-dd HH:mm:ss"));
Jsonobject jsonobj = jsonobject.fromobject (map, jsonconfig);

Class Datejsonvalueprocessor implements Jsonvalueprocessor {
Private String format = "YYYY-MM-DD";

Public Datejsonvalueprocessor () {
}

Public datejsonvalueprocessor (String format) {
This.format = format;
}

public object Processarrayvalue (object value, Jsonconfig jsonconfig) {
string[] obj = {};
if (value instanceof date[]) {
SimpleDateFormat SF = new SimpleDateFormat (format);
date[] dates = (date[]) value;
obj = new String[dates.length];
for (int i = 0; i < dates.length; i++) {
Obj[i] = Sf.format (Dates[i]);
}
}
return obj;
}

Public Object Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {
if (value instanceof Date) {
String str = new SimpleDateFormat (format). Format ((Date) value);
return str;
}
return value.tostring ();
}

Public String GetFormat () {
return format;
}

public void SetFormat (String format) {
This.format = format;
}

The above originates from the network

Java Time type conversion jsonvalueprocessor

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.