Did like below,
LocalDateTime currentUTCTime = LocalDateTime.now(ZoneId.of("UTC"));
String reqPattern = currentUTCTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));
System.out.println("Required pattern: " + reqPattern);
GregorianCalendar calendar = GregorianCalendar.from(currentUTCTime.atZone(ZoneId.systemDefault()));
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
System.out.println("But Showing As :" + xcal);
I want the output as 2015-06-18 11:59:15:135
, but when i set the xcal
to
a XML tag which takes XMLGregorianCalender
, it shows like 2015-06-18T11:59:15.135+05:30
.
How can i remove the +05:30
portion?
Use this code:
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 viatoString()
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, thejava.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 classXMLGregorianCalendar
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, usingcurrentUTCTime.format(DateTimeFormatter.ISO_DATE_TIME)
as suggested in this comment is also possible (instead of the shown hack).