[Java] How to Convert XMLGregorianCalendar to java.util.Date and The Other Way Around

This is a simple code snippet on how to convert XMLGregorianCalendar into java.util.Date and java.util.Date into javax.xml.datatype.XMLGregorianCalendar. Usually i found XMLGregorianCalendar everytime im trying to convert java bean into webservice using wsdl2java.

But there are some time when i need to convert those XMLGregorianCalendar objects and change them into java.util.Date so i could do a much more various process on them. So this is how i convert XMLGregorianCalendar into java.util.Date,

            
            XMLGregorianCalendar calendar = responseMessage.getDate();
            java.util.Date date = calendar.toGregorianCalendar().getTime();

and this is how i convert java.util.Date into XMLGregorianCalendar

            GregorianCalendar gregorianCalendar = new GregorianCalendar();
            gregorianCalendar.setTime(new Date());
            responseMessage.setDate(new XMLGregorianCalendarImpl(gregorianCalendar));

For example, im using XMLGregorianCalendar value “2012-10-15T00:00:00”. Hope it would help others (H)

1 Comment

Java Servlet Tutorial

about 5 years ago

Great tutorial. It's quite clear, keep going on!

Reply

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked