I am assuming that your
XMLGregorianCalendar holds a full date (year, month and day of month), a time of day (at least hour and minute) and a UTC offset (called timezone in the XML parlance) (each element is optional in
XMLGregorianCalendar, but all are needed for
OffsetDateTime). I am further assuming that your time of day has
not got more than 9 decimals on the seconds (nanosecond precision). For example.
There are some ways to go, each with their pros and cons.
Convert via String: I’d be tempted to do:
Output:
Pros: it’s short and hardly gives any surprises. Con: to me it feels like a waste formatting into a string and parsing back.
Others have already mentioned
converting via GregorianCalendar and ZonedDateTime:
Pro: it’s sort of the official conversion. Con: it’s longer.
If you want it complicated, you may also
convert via integers, only included for completeness:
Cons: it’s long, hard to read and easy to get wrong. If there is a pro, then: it’s the direct conversion.