The instance XMLGregorianCalendar attributes contains current date and time. - Java XML

Java examples for XML:XML Calendar

Description

The instance XMLGregorianCalendar attributes contains current date and time.

Demo Code


//package com.java2s;
import javax.xml.datatype.DatatypeConfigurationException;

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

import java.util.GregorianCalendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(createCurrentXmlCalendar());
    }/*from w w  w . j av a2 s.com*/

    /**
     * The instance XMLGregorianCalendar attributes contains current date and
     * time. Timezone and locale are taken from system defaults.
     *
     * @return instance of XMLGregorianCalendar
     */
    public static XMLGregorianCalendar createCurrentXmlCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        return convert2XmlCalendar(calendar);
    }

    /**
     * Converts java.util.GregorianCalendar into
     * javax.xml.datatype.XMLGregorianCalendar
     *
     * @param calendar
     *            the calendar
     * @return instance of XMLGregorianCalendar
     */
    public static XMLGregorianCalendar convert2XmlCalendar(
            GregorianCalendar calendar) {
        if (calendar == null) {
            return null;
        }

        try {
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(
                    calendar);
        } catch (DatatypeConfigurationException e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials