I have an instance of XMLGregorianCalendar with date format as
yyyy-MM-dd'T'HH:mm:ss
I need an instance of an XMLGregorianCalendar with
yyyy-MM-dd':'HH:mm:ss
date format. Is it possible?
I need to set this date in XML using JAXB where schema mandates that the field be XMLGregorianCalendar.
Convert the object of
XMLGregorianCalendarto an object ofZonedDateTimewhich you can format in the desired format usingDateTimeFormatter.Demo:
Output:
Now, you can use the string,
formattedin your XML.Note: A date-time object is supposed to store the information about date, time, timezone etc., not about the formatting. You can format a date-time object into a
Stringwith the pattern of your choice using date-time formatting API.java.time.format.DateTimeFormatter,java.time.format.DateTimeFormatterBuilderetc.) for the modern date-time types is in the package,java.time.format.java.text.SimpleDateFormat,java.text.DateFormatetc.) for the legacy date-time types is in the package,java.text.