How do you specify the date format used when JAXB marshals xsd:dateTime?

You can use an XmlAdapter to customize how a date type is written to XML.

package com.example;

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

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

public class DateAdapter extends XmlAdapter<String, Date> {

    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public String marshal(Date v) throws Exception {
        synchronized (dateFormat) {
            return dateFormat.format(v);

    public Date unmarshal(String v) throws Exception {
        synchronized (dateFormat) {
            return dateFormat.parse(v);


Then you use the @XmlJavaTypeAdapter annotation to specify that the XmlAdapter should be used for a specific field/property.

@XmlElement(name = "timestamp", required = true) 
protected Date timestamp; 

Using a xjb binding file:

<xjc:javaType name="java.util.Date" xmlType="xs:dateTime"

will produce the above mentioned annotation.
(By eventually adding the xjc namespace: xmlns:xjc="")

Leave a Comment