Java XML Date Create toXMLGregorianCalendar(long dateInMillis)

Here you can find the source of toXMLGregorianCalendar(long dateInMillis)

Description

to XML Gregorian Calendar

License

Open Source License

Declaration

public static XMLGregorianCalendar toXMLGregorianCalendar(long dateInMillis) 

Method Source Code


//package com.java2s;
/*/*  w w w.  ja v  a 2 s  .c  o m*/
 * Atricore IDBus
 *
 * Copyright (c) 2009, Atricore Inc.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * 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, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

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

import java.util.GregorianCalendar;

public class Main {
    private static final java.util.TimeZone tz = java.util.TimeZone.getTimeZone("UTC");

    public static XMLGregorianCalendar toXMLGregorianCalendar(long dateInMillis) {
        java.util.Date date = new java.util.Date(dateInMillis);
        return toXMLGregorianCalendar(date);
    }

    public static XMLGregorianCalendar toXMLGregorianCalendar(java.util.Date date) {

        DatatypeFactory datatypeFactory = newDatatypeFactory();
        GregorianCalendar gdate = new GregorianCalendar(tz);
        gdate.setTime(date);

        XMLGregorianCalendar calendar = datatypeFactory.newXMLGregorianCalendar(gdate);

        //        XMLGregorianCalendar calendar = datatypeFactory.newXMLGregorianCalendar(
        //                (date.getYear() + 1900),
        //                (date.getMonth() + 1),
        //                date.getDate(),
        //                date.getHours(),
        //                date.getMinutes(),
        //                date.getSeconds(),
        //                0,
        //                0 /* set to zulu UTC time */
        //        );

        return calendar;

    }

    private static DatatypeFactory newDatatypeFactory() {
        DatatypeFactory datatypeFactory;

        try {
            datatypeFactory = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException e) {
            throw new RuntimeException("Unable to initialize subscription", e);
        }

        return datatypeFactory;
    }
}

Related

  1. toXmlDateTimeUTC(Date date)
  2. toXMLGregorianCalendar(Date date)
  3. toXMLGregorianCalendar(Date date)
  4. toXMLGregorianCalendar(Date date, TimeZone timeZone, Locale locale)
  5. toXmlGregorianCalendar(GregorianCalendar calendar)
  6. toXMLGregorianCalendar(long timestamp)
  7. toXMLGregorianCalendar(String stringTypeDate)
  8. toXMLGregorianCalendar(Timestamp timestamp)
  9. xmlCalendarToCalendar(XMLGregorianCalendar cal)