Example usage for javax.xml.datatype XMLGregorianCalendar normalize

List of usage examples for javax.xml.datatype XMLGregorianCalendar normalize

Introduction

In this page you can find the example usage for javax.xml.datatype XMLGregorianCalendar normalize.

Prototype

public abstract XMLGregorianCalendar normalize();

Source Link

Document

Normalize this instance to UTC.

Usage

From source file:eu.europa.ec.markt.dss.DSSUtils.java

/**
 * Converts a given <code>Date</code> to a new <code>XMLGregorianCalendar</code>.
 *
 * @param date the date to be converted// ww w  .  ja  va 2 s  .  co m
 * @return the new <code>XMLGregorianCalendar</code> or null
 */
public static XMLGregorianCalendar createXMGregorianCalendar(Date date) {

    if (date == null) {
        return null;
    }

    final GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);

    try {
        XMLGregorianCalendar gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
        gc.setFractionalSecond(null);
        gc = gc.normalize(); // to UTC = Zulu
        return gc;
    } catch (DatatypeConfigurationException e) {

        // LOG.log(Level.WARNING, "Unable to properly convert a Date to an XMLGregorianCalendar",e);
    }

    return null;
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Converts a given {@code Date} to a new {@code XMLGregorianCalendar}.
 *
 * @param date the date to be converted//ww w .  j  ava2  s.com
 * @return the new {@code XMLGregorianCalendar} or null
 */
public static XMLGregorianCalendar createXMLGregorianCalendar(final Date date) {

    if (date == null) {
        return null;
    }
    final GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    try {

        XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(calendar);
        xmlGregorianCalendar.setFractionalSecond(null);
        xmlGregorianCalendar = xmlGregorianCalendar.normalize(); // to UTC = Zulu
        return xmlGregorianCalendar;
    } catch (DatatypeConfigurationException e) {
        LOG.warn("Unable to properly convert a Date to an XMLGregorianCalendar " + e.getMessage(), e);
    }
    return null;
}