我使用的java xml注释如下:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "OrganisationUnit", propOrder = {
"companyId",
"validFrom",
"validTo",
})
public class OrganisationUnit {
@XmlElement(name = "company_id", required = true)
protected String companyId;
@XmlElement(name = "valid_from")
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar validFrom;
@XmlElement(name = "valid_to")
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar validTo;
...
}
然而,当我通过REST服务提供信息时,date
元素会显示时区:
<organisationUnits>
<organisationUnit>
<company_id>ABC</company_id>
<valid_from>1970-01-01+01:00</valid_from>
<valid_to>2099-12-31+01:00</valid_to>
</organisationUnit>
</organisationUnits>
我不完全理解是否需要XmlSchemaType date
来显示时区,但理想情况下,我希望显示没有时区的日期。
<valid_from>1970-01-01</valid_from>
<valid_to>2099-12-31</valid_to>
我该怎么做?
好吧,阅读文档有时会告诉你美妙的事情,下面是如何做到这一点的,时区DatatypeConstants.FIELD_UNDEFINED
:
private XMLGregorianCalendar dateWithoutTimezone(Date date) throws DatatypeConfigurationException {
calendar.setTime(date);
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
xmlGregorianCalendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
return xmlGregorianCalendar;
}
希望这能有所帮助。