Java XML Date Create createXmlGregorianCalendar(Date date)

Here you can find the source of createXmlGregorianCalendar(Date date)

Description

Converts the data to an XMLGregorianCalendar in the GMT time zone.

License

Open Source License

Declaration

public static XMLGregorianCalendar createXmlGregorianCalendar(Date date) 

Method Source Code

//package com.java2s;
/*/*from  ww  w  .j  ava 2 s .  co  m*/
 * eID PKI RA Project.
 * Copyright (C) 2010-2014 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.
 */

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

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

public class Main {
    private static DatatypeFactory datatypeFactory;

    /**
     * Converts the data to an XMLGregorianCalendar in the GMT time zone.
     */
    public static XMLGregorianCalendar createXmlGregorianCalendar(Date date) {
        if (date == null) {
            return null;
        }
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));

        return getDatatypeFactory().newXMLGregorianCalendar(calendar);
    }

    /**
     * Returns the data type factory to create exotic XML objects..
     */
    public synchronized static DatatypeFactory getDatatypeFactory() {
        if (datatypeFactory != null) {
            return datatypeFactory;
        }

        try {
            datatypeFactory = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException e) {
            throw new RuntimeException("Cannot get DatatypeFactory", e);
        }
        return datatypeFactory;
    }
}

Related

  1. createXMLGregorianCalendar(Date currentDateTime)
  2. createXMLGregorianCalendar(final Date date)
  3. createXMLGregorianCalendar(final Date time)
  4. dateToString(Calendar calendar)
  5. dateToXml(Date date)