Monday, June 11, 2012

java.util.Date to XMLGregorianCalendar


Isn't there a convenient way of getting from a java.util.Date to a XMLGregorianCalendar?



Source: Tips4all

6 comments:

  1. GregorianCalendar c = new GregorianCalendar();
    c.setTime(yourDate);
    XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);

    ReplyDelete
  2. For those that might end up here looking for the oposite convertion (XMLGregorianCalendar to Date):

    XMLGregorianCalendar xcal = <assume this is initialized>;
    java.util.Date dt = xcal.toGregorianCalendar().getTime();

    ReplyDelete
  3. Here is a method for converting from a GregorianCalendar to XMLGregorianCalendar; I'll leave the part of converting from a java.util.Date to GregorianCalendar as an exercise for you:

    import java.util.GregorianCalendar;

    import javax.xml.datatype.DatatypeFactory;
    import javax.xml.datatype.XMLGregorianCalendar;

    public class DateTest {

    public static void main(final String[] args) throws Exception {
    GregorianCalendar gcal = new GregorianCalendar();
    XMLGregorianCalendar xgcal = DatatypeFactory.newInstance()
    .newXMLGregorianCalendar(gcal);
    System.out.println(xgcal);
    }

    }


    EDIT: Slooow :-)

    ReplyDelete
  4. I hope my encoding here is right ;D
    To make it faster just use the ugly getInstance() call of GregorianCalendar instead of constructor call:



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

    public class DateTest {

    public static void main(final String[] args) throws Exception {
    // do not forget the type cast :/
    GregorianCalendar gcal = (GregorianCalendar) GregorianCalendar.getInstance();
    XMLGregorianCalendar xgcal = DatatypeFactory.newInstance()
    .newXMLGregorianCalendar(gcal);
    System.out.println(xgcal);
    }

    }

    ReplyDelete
  5. Just thought I'd add my solution below, since the answers above did not meet my exact needs. My Xml schema required seperate Date and Time elements, not a singe DateTime field. The standard XMLGregorianCalendar constructor used above will generate a DateTime field

    Note there a couple of gothca's, such as having to add one to the month (since java counts months from 0).

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(yourDate);
    XMLGregorianCalendar xmlDate = df.newXMLGregorianCalendarDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH), 0);
    XMLGregorianCalendar xmlTime = df.newXMLGregorianCalendarTime(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), 0);

    ReplyDelete
  6. I agree that it is better to use the getInstance method, but how can the "getInstance()" be faster than the constructor method?

    The getInstance() method calls the method "createCalendar(TimeZone zone, Locale aLocale)" which returns a "new GregorianCalendar(zone, aLocale)"!

    ReplyDelete