Skip to main content

How to map java.sql.Date datatype to "yyyy-mm-dd"

2 replies [Last post]
wangzaixiang
Offline
Joined: 2004-11-24

It looks JAXB2 maps java.sql.Date to a String "yyyy-mm-ddThh:mm:ss" format, can we redefine it to other format such as "yyyy-mm-dd"

In my application, there is a lot of persistence entity and a lot of field has the type: java.sql.Date.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
wangzaixiang
Offline
Joined: 2004-11-24

I have find how to resolve the problem.

By define a DateXmlAdaptor(extends XmlAdaptor), and register it in the package-info.java's @XmlJavaTypeAdaptor annotation.

It looks well and work well. Thanks for the JAXB2 's power.

public static class DateAdaptor extends XmlAdapter{

public Date unmarshal(String v) throws Exception {
return Date.valueOf(v);
}

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

}

kohsuke
Offline
Joined: 2003-06-09

Yup. That's the way to go. Thank you for sharing your solution.