如何指定 JAXB 封送 xsd: dateTime 时使用的日期格式?

当 JAXB 将日期对象(XMLGregorianCalendar)封送到 xsd: dateTime 元素中时。如何指定结果 XML 的格式?

例如: 默认数据格式使用毫秒 <StartDate>2012-08-21T13:21:58.000Z</StartDate> 我需要省略毫秒

如何指定要使用的输出表单/日期格式? 我使用 javax.xml.datatype.DatatypeFactory来创建 XMLGregorianCalendar对象。

XMLGregorianCalendar xmlCal = datatypeFactory.newXMLGregorianCalendar(cal);
106371 次浏览

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");


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


@Override
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)
@XmlJavaTypeAdapter(DateAdapter.class)
protected Date timestamp;

Using a xjb binding file:

<xjc:javaType name="java.util.Date" xmlType="xs:dateTime"
adapter="com.example.DateAdapter"/>

will produce the above mentioned annotation.
(By eventually adding the xjc namespace: xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc")

I use a SimpleDateFormat to create the XMLGregorianCalendar, such as in this example:

public static XMLGregorianCalendar getXmlDate(Date date) throws DatatypeConfigurationException {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd").format(date));
}


public static XMLGregorianCalendar getXmlDateTime(Date date) throws DatatypeConfigurationException {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(date));
}

The first method creates an instance of XMLGregorianCalendar that is formatted by the XML marshaller as a valid xsd:date, the second method results in a valid xsd:dateTime.

Very easy way to me. Formatting XMLGregorianCalendar for marshalling in java.

I just create my data in the good format. The toString will be called producing the good result.

public static final XMLGregorianCalendar getDate(Date d) {
try {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd").format(d));
} catch (DatatypeConfigurationException e) {
return null;
}
}

https://www.baeldung.com/jaxb

public class DateAdapter extends XmlAdapter<String, Date> {


private static final ThreadLocal<DateFormat> dateFormat
= new ThreadLocal<DateFormat>() {


@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}


@Override
public Date unmarshal(String v) throws Exception {
return dateFormat.get().parse(v);
}


@Override
public String marshal(Date v) throws Exception {
return dateFormat.get().format(v);
}
}

Usage:

import com.company.LocalDateAdapter.yyyyMMdd;


//...


@XmlElement(name = "PROC-DATE")
@XmlJavaTypeAdapter(yyyyMMdd.class)
private LocalDate processingDate;

LocalDateAdapter

import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


public class LocalDateAdapter extends XmlAdapter<String, LocalDate> {


public static final class yyyyMMdd extends LocalDateAdapter {
public yyyyMMdd() {
super("yyyyMMdd");
}
}


public static final class yyyy_MM_dd extends LocalDateAdapter {
public yyyy_MM_dd() {
super("yyyy-MM-dd");
}
}


private final DateTimeFormatter formatter;


public LocalDateAdapter(String pattern) {
formatter = DateTimeFormat.forPattern(pattern);
}


@Override
public String marshal(LocalDate date) throws Exception {
return formatter.print(date);
}


@Override
public LocalDate unmarshal(String date) throws Exception {
return formatter.parseLocalDate(date);
}
}