Convert XMLGregorianCalendar to Date in Java

This tutorial shows you how to convert XMLGregorianCalendar to Date in Java. It is popular case that you usually catch up while working with XML in Java. By using Java API, you can easily do this conversion of XMLGregorianCalendar to Date and Date to XMLGregorianCalendar.

Convert XMLGregorianCalendar to Date

We are together to create the “Convert XMLGregorianCalendar to Date” and “Convert Date to XMLGregorianCalendar” examples like below:

Convert Date to XMLGregorianCalendar

Convert XMLGregorianCalendar to Date example is a java program, which convert XMLGregorianCalendar instances to java.util.Date instances. Let see detail the snippet below:

Here is output

Convert XMLGregorianCalendar to Date

Here is output

Note some important points about XMLGregorianCalendar:

1) XML Schema supports three data types such as date, time and datetime and defined by three tags xsd:date, xsd:time and xsd:dateTime. While working with XML using JAXB framework then JAXB XJC maps all these types to XMLGregorianCalendar.

2) It’s possible to define xml schema for converting XMLGregorianCalendar to Date. The XJC(Binding compiler) will generate Date instead of XMLGregorianCalendar. I will show this way for you soon.

3) One more important thing, you should use constructor to create instance of GregorianCalendar instead of GregorianCalendar.getInstance(). Because it’s same as Calendar.getInstance() and can return different instances base on Local settings.

That’s it. Hope that you could catch up how to convert XMLGregorianCalendar to Date in Java and Date to XMLGregorianCalendar. You can also refer to the Complete Java XML tutorial to get more Java Xml knowledge.

Happy learing!

Leave a Comment

*

Please share it if you found this useful
Hide Buttons