Thursday, September 22, 2011

Converting from java.util.Date to XMLGregorianCalendar

If you need to convert from java.util.date to an XMLGregorianCalendar format you can use the following code:

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import java.util.GregorianCalendar;

public class DateConversion {
   public XMLGregorianCalendar dateToXMLGregorianCalendar(
   final java.util.Date date)
   throws DatatypeConfigurationException {
      DatatypeFactory df = DatatypeFactory.newInstance();
      if (date == null) {
         return null;
      } else {
         GregorianCalendar gc = new GregorianCalendar();
         gc.setTimeInMillis(date.getTime());
         return df.newXMLGregorianCalendar(gc);
      }
   }
}

No comments:

Post a Comment