Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert between LocalDate and XMLGregorianCalendar

What's the best way to convert between LocalDate from Java 8 and XMLGregorianCalendar?

like image 692
maja Avatar asked Apr 21 '15 08:04

maja


People also ask

How do you convert GregorianCalendar to XMLGregorianCalendar?

DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .

What is XMLGregorianCalendar?

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.

Can we convert LocalDate to date?

Convert LocalDate To Date in Java Step 1: First Create the LocalDate object using either now() or of() method. now() method gets the current date where as of() creates the LocalDate object with the given year, month and day. Step 2: Next, Get the timezone from operating system using ZoneId. systemDefault() method.


2 Answers

Converting from LocalDate to XMLGregorianCalendar:

LocalDate date = LocalDate.now(); GregorianCalendar gcal = GregorianCalendar.from(date.atStartOfDay(ZoneId.systemDefault())); XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal); 

Converting back is simpler:

xcal.toGregorianCalendar().toZonedDateTime().toLocalDate(); 
like image 102
JodaStephen Avatar answered Oct 14 '22 15:10

JodaStephen


The LocalDate stores only year/month/day information. There is no time nor time-zone information in it. The XMLGregorianCalendar stores date (year/month/day) + optionally time and optionally time zone information.

So converting from LocalDate to XMLGregorianCalendar is simple:

LocalDate in; XMLGregorianCalendar out; in = LocalDate.parse("1999-11-11"); out = DatatypeFactory.newInstance().newXMLGregorianCalendar(in.toString()); 

Converting from XMLGregorianCalendar to LocalDate might be not so simple, because XMLGregorianCalendar may have time and time-zone information which you simply can't store in LocalDate.

However, I guess that if you are converting from XMLGregorianCalendar to LocalDate then the XMLGregorianCalendar is resulting from a nontimezoned xsd:date element (represented as YYYY-MM-DD in the xml). In that case you should convert it like this:

XMLGregorianCalendar in; LocalDate out; in = DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-11-11"); out = LocalDate.parse(in.toXMLFormat()); 

Whole example:

    {         LocalDate in;         XMLGregorianCalendar out;         in = LocalDate.parse("1999-11-11");         out = DatatypeFactory.newInstance().newXMLGregorianCalendar(in.toString());         System.out.println("in: " + in.toString());         System.out.println("out: " + out.toXMLFormat());     }     {         XMLGregorianCalendar in;         LocalDate out;         in = DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-11-11");         out = LocalDate.parse(in.toXMLFormat());         System.out.println("in: " + in.toXMLFormat());         System.out.println("out: " + out.toString());     } 
like image 22
riskop Avatar answered Oct 14 '22 16:10

riskop