Birth Date with Jaxb and XMLGregorianCalendar

Lots of googling and frustrated colleagues and no solution to represent a simple birth date without minutes and seconds. In short, jaxb always uses XMLGregorianCalendar to represent dates, how to represent the birth date as
instead of

This is related to my previous article regarding Fødselsnummer, the Norwegian equivalent of US social security number, extracting birth date from the fødselsnummer.

As this jax-rpc vs jaxb comparison shows,, all date related stuff is represented as XMLGregorianCalendar in JAXB. Normally, I work with org.joda.time.DateTime and map to the XMLGregorianCalendar like this.

public static XMLGregorianCalendar dateTimeToXMLGregorianCalendar(
                       DateTime dateTime) throws DatatypeConfigurationException {
  DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
  return dataTypeFactory.newXMLGregorianCalendar(dateTime.toGregorianCalendar());

What I’ve learned is that it’s of great importance how the XMLGregorianCalendar object is constructed. The solution is simple, but I argue that it’s not very intuitive. I like explicit constructors or static constructors to indicate separate constructors.

public static XMLGregorianCalendar birthDateToXML(DateTime birthDate) throws DatatypeConfigurationException {
        XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
        return xmlGregorianCalendar;

now you may use xsd:date to represent it, instead of xsd:dateTime, in order to improve compatibility with older web services implementing older jaxb versions (with java.util.calendar instead of XMLGregorianCalendar) and/or JAX-RPC.

About Ole Morten Amundsen

Developer, programmer, entrepreneur. Java, .Net, ruby, rails, agile, lean. Opinionated enthusiast!
This entry was posted in Java and tagged , , , , . Bookmark the permalink.

12 Responses to Birth Date with Jaxb and XMLGregorianCalendar

  1. kuhajeyan says:


  2. roberto says:

    I used in this way and work fine:

    @XmlElement @XmlSchemaType(name=”date”) private Date birthDate;

    • Ole Morten says:

      Did you get that code snippet auto-generated from the xsd using jaxb2? If so, cool! Can I see the xsd? :)

  3. kamaraju says:

    After generating the xml i should get the date format in yyyy-mm-dd not with time zone .will it come so …

    • Ole Morten says:

      That’s what the post is about and the goal is clearly stated in the first paragraph, the same as your goal, right? Hint: Don´t populate the timezone object (null) in XMLGregorianCalendar.

  4. Oscar.o says:

    Thx man- You saved my life

  5. Heike says:

    I was searching for this solution for days. Finally found the answer here. Thanks!

  6. JKC says:

    Thanks for the solution. It works for me if I use xsd:date.

    I tried using this solution with xsd:dateTime but I can only get it to work with xsd:date. If I use xsd:dateTime the date doesn’t get converted to XML and the field is empty.

    Do you have any idea why?

  7. Anonymous says:


    is faster

  8. Thank you for posting this :)
    ~pasquale from Italy

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s