Is there any short way to convert java.time.ZonedDateTime to XMLGregorianCalendar?
Maybe I need some intermediate steps like convert ZonedDateTime to java.util.Date, but that will make code too messy.
The problem appeared in JAX-WS web services, datetime there is passed as XMLGregorianCalendar.
now() now() method of a ZonedDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return ZonedDateTime based on system clock with default time-zone to obtain the current date-time. The zone and offset will be set based on the time-zone in the clock.
The XML Schema standard defines clear rules for specifying dates in XML format. In order to use this format, the Java class XMLGregorianCalendar, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes.
The simplest way to format XMLGregorianCalendar is to first convert it to the Date object, and format the Date to String. XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal. toGregorianCalendar(). getTime(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z"); String formattedString = df.
If you want the last second of the day, you can use: ZonedDateTime eod = zonedDateTime. with(LocalTime. of(23, 59, 59));
At the current moment I think it's the most straightforward way to do it:
ZonedDateTime now = ZonedDateTime.now();
GregorianCalendar gregorianCalendar = GregorianCalendar.from(now);
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With