Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain XMLGregorianCalendar date format after converting to JSON - Jackson Lib

Tags:

java

json

jackson

I have an object that has 2 XMLGregorianCalendar objects - one is used for date and other is used for time. I use Jackson object mapper to convert the date in JSON format. The date before converting is 2014-02-10 & time is 11:15:00. After converting to JSON , it becomes {"date":1392008400000,"time":58500000}. How can I retain the same date & time format ({"date":2014-02-10,"time":11:15:00}) after printing in 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;
    }



}

WORKING CODE (With 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();

}
like image 641
Punter Vicky Avatar asked Mar 15 '23 13:03

Punter Vicky


1 Answers

Here

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

you're printing a Date object formatted through its toString() method.

Here

System.out.println(" Before Converting " + xmlDate);

you're printing an XMLGregorianCalendar object formatted through its toString() method.

Here

System.out.println("After Converting " + mapper.writeValueAsString(xmlDate));

you're printing a String which represents a XMLGregorianCalendar converted to a JSON value through ObjectMapper's default conversion, which simply takes the timestamp (time in millis since epoch).

None of these things are the same.

If you want your ObjectMapper to serialize date types differently, use its ObjectMapper#setDateFormat(DateFormat) method as required.

There's also SerializationFeature.WRITE_DATES_AS_TIMESTAMPS which is enabled by default.

like image 115
Sotirios Delimanolis Avatar answered Apr 26 '23 08:04

Sotirios Delimanolis