Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert java.time.ZonedDateTime to XMLGregorianCalendar?

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.

like image 564
Mikhail Boyarsky Avatar asked Apr 23 '14 08:04

Mikhail Boyarsky


People also ask

How do I get time from ZonedDateTime?

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.

What is XMLGregorianCalendar in java?

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.

How do I create an instance of XMLGregorianCalendar?

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.

How do I get end of the day from ZonedDateTime?

If you want the last second of the day, you can use: ZonedDateTime eod = zonedDateTime. with(LocalTime. of(23, 59, 59));


1 Answers

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);
like image 176
Mikhail Boyarsky Avatar answered Oct 01 '22 03:10

Mikhail Boyarsky