JAXB: Date Format Example in Java

Connect with

Oracle Java
You know , java.util.Date is the complex object, and parsing from string to Date is little bit complex. but don’t worry, it can be simple once you know the basic things.

JAXB supports to customize marshaling and de-marshaling of java.util.Date object, meaning that , you can customize your own format of data in xml and 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.

1. Step by step approach

first of all, You need to think how to convert Date to String and String to 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 marshal method, while converting Java Object to XML document, and unmarshal method to bind XML document to Java object.

Sample XML



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

2. POJO class

File: Person.java, this is a POJO class.

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 + "}"; 
  } 
 }

3. 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 main responsibility 
 * to convert from java.util.Date to 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);
 }
}

4. 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.

5. Key Points of XmlJavaTypeAdapter

  • In this post you learnt how to write customer 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.

Your comments are welcome to improve this post. Happy Learning 🙂


Connect with

1 thought on “JAXB: Date Format Example in Java

  1. Pingback: Anonymous

Leave a Reply

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