Marshalling LocalDate using JAXB

You will have to create an XmlAdapter like this:

public class LocalDateAdapter extends XmlAdapter<String, LocalDate> {
    public LocalDate unmarshal(String v) throws Exception {
        return LocalDate.parse(v);
    }

    public String marshal(LocalDate v) throws Exception {
        return v.toString();
    }
}

And annotate your field using

 @XmlJavaTypeAdapter(value = LocalDateAdapter.class)

See also javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters if you want to define your adapters on a package level.

Leave a Comment