Java XML Transform Usage getXMLCalendar(Date date)

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

Description

Transform a Java date into a XML calendar.

License

Apache License

Parameter

Parameter Description
date a parameter

Declaration

public static XMLGregorianCalendar getXMLCalendar(Date date) 

Method Source Code

//package com.java2s;
/**//from w w w .j a va2 s . c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.joda.time.DateTime;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

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

import java.util.TimeZone;

public class Main {
    private static DatatypeFactory datatypeFactory;

    /**
     * Transform a Java date into a XML calendar. Useful for working with date literals.
     * @param date
     * @return
     */
    public static XMLGregorianCalendar getXMLCalendar(Date date) {
        GregorianCalendar c = new GregorianCalendar();
        c.setTimeZone(TimeZone.getTimeZone("UTC"));
        c.setTime(date);
        try {
            return getDatatypeFactory().newXMLGregorianCalendar(c);
        } catch (DatatypeConfigurationException e) {
            return null;
        }
    }

    /**
     * Transform a Java date into a XML calendar. Useful for working with date literals.
     * @param date
     * @return
     */
    public static XMLGregorianCalendar getXMLCalendar(DateTime date) {
        GregorianCalendar c = date.toGregorianCalendar();
        try {
            return getDatatypeFactory().newXMLGregorianCalendar(c);
        } catch (DatatypeConfigurationException e) {
            return null;
        }
    }

    private static DatatypeFactory getDatatypeFactory() throws DatatypeConfigurationException {
        if (datatypeFactory == null) {
            datatypeFactory = DatatypeFactory.newInstance();
        }
        return datatypeFactory;
    }
}

Related

  1. getSchema(InputStream... inputs)
  2. getSourceFromEndpointReference(EndpointReference epr)
  3. getStreamSources(File[] stylesheets)
  4. getTemplatesByName(File xslF)
  5. getWSAAddress(W3CEndpointReference ref)
  6. getXmlEventWriter(Result r)
  7. longToGregorian(long date)
  8. map2Xml(Map map)
  9. newSchema(File xmlSchema)