转换为JSON后保留XMLGregorianCalendar日期格式--Jackson Lib

时间:2015-10-08 15:34:30

标签: java json jackson

我有一个包含2个XMLGregorianCalendar对象的对象 - 一个用于日期,另一个用于时间。我使用Jackson对象映射器以JSON格式转换日期。转换前的日期是2014-02-10&时间是11:15:00。转换为JSON后,它变为{“date”:1392008400000,“time”:58500000}。我怎样才能保留相同的日期和时间时间格式({“日期”:2014-02-10,“时间”:11:15:00})在JSON中打印后。

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;


public class TestJSON {

    public static void main(String[] args){

        Date dob=null;
        Date time=null;
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        DateFormat df1=new SimpleDateFormat("hh:mm:ss");
        try {
            dob=df.parse("2014-02-10");
            time=df1.parse("11:15:00");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(" Date = " + dob);
        System.out.println(" Time = " + time);

        GregorianCalendar cal = new GregorianCalendar();
        GregorianCalendar cal1 = new GregorianCalendar();
        try {
            cal.setTime(df.parse("2014-02-10"));
            cal1.setTime(df1.parse("11:15:00"));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        XMLGregorianCalendar xmlDate = null;
        XMLGregorianCalendar xmlDate1 = null;
        try {
            xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
            xmlDate1 = DatatypeFactory.newInstance().newXMLGregorianCalendarTime(cal1.get(Calendar.HOUR),cal1.get(Calendar.MINUTE),cal.get(Calendar.SECOND), DatatypeConstants.FIELD_UNDEFINED);
        } catch (DatatypeConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println(" Before Converting - Date " + xmlDate);
        System.out.println(" Before Converting - Time " + xmlDate1);

        Data data = new Data();
        data.setDate(xmlDate);
        data.setTime(xmlDate1);



        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println("After Converting " + mapper.writeValueAsString(data));
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


}




import javax.xml.datatype.XMLGregorianCalendar;

public class Data {

    private XMLGregorianCalendar date;
    private XMLGregorianCalendar time;

    public XMLGregorianCalendar getDate() {
        return date;
    }
    public void setDate(XMLGregorianCalendar date) {
        this.date = date;
    }
    public XMLGregorianCalendar getTime() {
        return time;
    }
    public void setTime(XMLGregorianCalendar time) {
        this.time = time;
    }



}

工作代码(使用Mixins)

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;

    import javax.xml.datatype.DatatypeConfigurationException;
    import javax.xml.datatype.DatatypeConstants;
    import javax.xml.datatype.DatatypeFactory;
    import javax.xml.datatype.XMLGregorianCalendar;

    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;


    public class TestJSON {

        public static void main(String[] args){

            Date dob=null;
            Date time=null;
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            DateFormat df1=new SimpleDateFormat("hh:mm:ss");
            try {
                dob=df.parse("2014-02-10");
                time=df1.parse("11:15:00");
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println(" Date = " + dob);
            System.out.println(" Time = " + time);

            GregorianCalendar cal = new GregorianCalendar();
            GregorianCalendar cal1 = new GregorianCalendar();
            try {
                cal.setTime(df.parse("2014-02-10"));
                cal1.setTime(df1.parse("11:15:00"));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            XMLGregorianCalendar xmlDate = null;
            XMLGregorianCalendar xmlDate1 = null;
            try {
                xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
                xmlDate1 = DatatypeFactory.newInstance().newXMLGregorianCalendarTime(cal1.get(Calendar.HOUR),cal1.get(Calendar.MINUTE),cal.get(Calendar.SECOND), DatatypeConstants.FIELD_UNDEFINED);
            } catch (DatatypeConfigurationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println(" Before Converting - Date " + xmlDate);
            System.out.println(" Before Converting - Time " + xmlDate1);

            Data data = new Data();
            data.setDate(xmlDate);
            data.setTime(xmlDate1);



            ObjectMapper mapper = new ObjectMapper();
            try {
                mapper.addMixIn(Data.class, MyMixin.class);
                System.out.println("After Converting " + mapper.writeValueAsString(data));

            } catch (JsonProcessingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }


    }


import javax.xml.datatype.XMLGregorianCalendar;

public class Data {

    private XMLGregorianCalendar date;
    private XMLGregorianCalendar time;

    public XMLGregorianCalendar getDate() {
        return date;
    }
    public void setDate(XMLGregorianCalendar date) {
        this.date = date;
    }
    public XMLGregorianCalendar getTime() {
        return time;
    }
    public void setTime(XMLGregorianCalendar time) {
        this.time = time;
    }



}



import javax.xml.datatype.XMLGregorianCalendar;

import com.fasterxml.jackson.annotation.JsonFormat;

public interface MyMixin {

    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
    public XMLGregorianCalendar getDate(); 
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="HH:mm:ss")
    public XMLGregorianCalendar getTime();

}

1 个答案:

答案 0 :(得分:5)

下面

<binding messageEncoding="Text">
  <readerQuotas maxDepth="2000000" />
</binding>
<binding name="SpecificBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
  <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>

您正在打印通过其System.out.println(" Date = " + dob); System.out.println(" Time = " + time); 方法格式化的Date对象。

下面

toString()

您正在打印通过其System.out.println(" Before Converting " + xmlDate); 方法格式化的XMLGregorianCalendar对象。

下面

toString()

您正在打印System.out.println("After Converting " + mapper.writeValueAsString(xmlDate)); ,表示String通过XMLGregorianCalendar的默认转化转换为JSON值,该转换只需要时间戳(自纪元以来的毫秒时间)。

这些都不一样。

如果您希望ObjectMapper以不同方式序列化日期类型,请根据需要使用其ObjectMapper#setDateFormat(DateFormat)方法。

默认情况下还会启用SerializationFeature.WRITE_DATES_AS_TIMESTAMPS