Use this code:
LocalDateTime currentUTCTime = LocalDateTime.now(); // using system timezone
String iso = currentUTCTime.toString();
if (currentUTCTime.getSecond() == 0 && currentUTCTime.getNano() == 0) {
iso += ":00"; // necessary hack because the second part is not optional in XML
}
XMLGregorianCalendar xml =
DatatypeFactory.newInstance().newXMLGregorianCalendar(iso);
Explanation:
The code makes use of the given factory method expecting a lexicographical representation of a local timestamp in ISO-8601-format. And since a LocalDateTime
does not refer to any timezone, its output via toString()
cannot contain a timezone offset. Result: XMLGregorianCalendar
considers the timezone offset as "not set".
Correction:
The original code did not especially bother about the ISO-variant of formatted output of currentUTCTime.toString()
. However, the java.time-API
produces an output without seconds or nanoseconds if those parts are equal to zero. This is perfectly legal in ISO, but the W3C-consortium has made the second part non-optional. And the class XMLGregorianCalendar
closely follows this deviating spec. Therefore the shown hack above using simple string concatenation in this special edge case. Thanks a lot to @Dave's comment. By the way, using currentUTCTime.format(DateTimeFormatter.ISO_DATE_TIME)
as suggested in this comment is also possible (instead of the shown hack).
DatatypeFactory.newInstance().newXMLGregorianCalendar(currentUTCTime.toString())
using the lexical representation in ISO-8601? – Airboat