JAXB Date Format example in Java

Connect with

JAXB Date Format Example in JavaJAXB Date Format Example in Java, using @XmlJavaTypeAdapter annotation. Write your own MyDateAdapter by extending XmlAdapter. Date is an object, and parsing from string to java.util.Date

1. Overview of JAXB date format

JAXB supports to customize marshaling and de-marshaling of java.util.Date object using annotation. You can customize your own format of date in xml and java object. You have to defined XmlAdapter which control the customization of your data format.

You can use SimpleDateFormat class to format your java.util.Date, formatting logic will reside in your custom class i.e.MyDateAdapter which extends XmlAdapter class.

2. Approach for JAXB Date format

first of all, You need to think about how to convert Date to String and String to java.util.Date

  • first of all, write POJO class.
  • write your own MyDateAdapter by extending XmlAdapter
  • overrides marshal(Date object) and unmarshal(String xml) methods.
  • JAXB calls the marshal method while converting Java Object to XML document, and the unmarshal method to bind XML document to Java object.

Sample XML for Date format



    Ranjeet Jha
    15-Jan-1980 18:30:00

3. POJO class for JAXB date format

In this section, you learn about how JAXB date format annotation cofigured using @XmlJavaTypeAdapter in java code.
File: Person.java, this is a POJO class used for JAXB date formate .

package com.mysoftkey.jaxb.adapter;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * This is POJO class for Person object.
 * 
 * @author ranjeet.jha
 *
 */
@XmlRootElement(name="person") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Person { 
 
 @XmlElement(name="name") 
 private String name; 
 
 @XmlElement(name="dateOfBirth") 
 @XmlJavaTypeAdapter(MyDateAdapter.class)
 private Date dateOfBirth; 
 
 // no-arg default constructor for JAXB 
 public Person(){} 
 
 public Person(String name, Date dateOfBirth) { 
  this.name = name; 
  this.dateOfBirth = dateOfBirth; 
  } 

 public Date getDateOfBirth() { 
  return dateOfBirth; 
  } 
 public void setDateOfBirth(Date dateOfBirth) {
  this.dateOfBirth = dateOfBirth; 
  }
 
 public String getName() { return name; } 
 
 public void setName(String name) {
  this.name = name; 
  } 
 
 @Override public String toString() { 
  return "{name : " + name + ", dateOfBirth : " + dateOfBirth + "}"; 
  } 
 }

4. Custom XmlAdapter Class

package com.mysoftkey.jaxb.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * This is Adaptor class which has the main responsibility 
 * to convert from java.util.Date to a format string of date.
 * 
 * @author ranjeet.jha
 *
 */
public class MyDateAdapter extends XmlAdapter {
 private final DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");

 @Override
 public Date unmarshal(String xml) throws Exception {
  return dateFormat.parse(xml);
 }

 @Override
 public String marshal(Date object) throws Exception {
  return dateFormat.format(object);
 }
}

5. Java Mail Application

This is mail java class , i.e. entry point of the demo example.

package com.mysoftkey.jaxb.adapter;

import java.io.File;
import java.io.StringWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * This example class is used to formate XML representation date to java
 * representation date. and vice-versa using JAXB.
 * 
 *
 * @author ranjeet.jha
 */
public class JAXBDateFormatExample {

 public static void main(String args[]) {

  try {
   Date dob = new GregorianCalendar(1980, Calendar.JANUARY, 15, 18, 30).getTime();

   Person person = new Person("Ranjeet Jha", dob);

   // Marshaling Employee object to XML using JAXB
   JAXBContext ctx = null;
   StringWriter writer = new StringWriter();

   ctx = JAXBContext.newInstance(Person.class);
   Marshaller m = ctx.createMarshaller();
   /*
    * System.out.println("Person object as XML"); System.out.println(writer);
    */
   m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

   // Write to System.out
   m.marshal(person, System.out);

   // Write to File, under docs which is inside the project
   m.marshal(person, new File("./docs/person.xml"));

  } catch (JAXBException ex) {
   ex.printStackTrace();
  }
 }

}

Output:



    Ranjeet Jha
    15-Jan-1980 18:30:00

this is the output of the example code , you can write logic to read via demarshalling it.

6. Key Points of XmlJavaTypeAdapter

  • In this post you learnt how to write customer JAXB date format to string formated output. It means all about, how to format java.util.Date in JAXB.
  • We have not only learned Date formatting during marshaling of the Date object but also seen how to customize JAXB marshaling and unmarshalling process.
  • This technique can be used to customize marshaling of any Java type e.g. BigDecimal, float, or double etc by using annotation @XmlJavaTypeAdapter which will specify in your custom date Adapter.

6. Reference

oracle.com
I hope you enjoyed this post about JAXB date format annotation using @XMLTypeAdapter, visit Core Java tutorial for more blog post.

Your comment is welcome to improve this post. Happy Learning! 🙂


Connect with

1 thought on “JAXB Date Format example in Java”

  1. Pingback: Anonymous

Leave a Comment

Your email address will not be published. Required fields are marked *