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
XMLGregorianCalendar
to an object ofZonedDateTime
which you can format in the desired format usingDateTimeFormatter
.Demo:
Output:
Now, you can use the string,
formatted
in 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
String
with the pattern of your choice using date-time formatting API.java.time.format.DateTimeFormatter
,java.time.format.DateTimeFormatterBuilder
etc.) for the modern date-time types is in the package,java.time.format
.java.text.SimpleDateFormat
,java.text.DateFormat
etc.) for the legacy date-time types is in the package,java.text
.