date To XML Gregorian Calendar - Java XML

Java examples for XML:XML Calendar

Description

date To XML Gregorian Calendar

Demo Code


//package com.java2s;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Date date = new Date();
        System.out.println(dateToXMLGregorianCalendar(date));
    }/*ww w.j  ava 2  s  .c o m*/

    public static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date)
            throws ParseException, DatatypeConfigurationException {
        XMLGregorianCalendar result = null;
        GregorianCalendar gregorianCalendar;

        gregorianCalendar = (GregorianCalendar) GregorianCalendar
                .getInstance();
        gregorianCalendar.setTime(date);
        result = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
                gregorianCalendar.get(Calendar.YEAR),
                gregorianCalendar.get(Calendar.MONTH) + 1,
                gregorianCalendar.get(Calendar.DAY_OF_MONTH),
                DatatypeConstants.FIELD_UNDEFINED);
        return result;
    }
}

Related Tutorials