title: Retain XMLGregorianCalendar date format after converting to JSON - Jackson Lib
body: <p>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 &amp; time is 11:15:00. After converting to JSON , it becomes {"date":1392008400000,"time":58500000}. How can I retain the same date &amp; time format ({"date":2014-02-10,"time":11:15:00}) after printing in JSON. .</p>
  
  <pre><code>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;
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>WORKING CODE (With Mixins)</strong></p>
  
  <pre><code>    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();
  
  }
  </code></pre>