All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crabshue.commons.xml.XmlDateUtils Maven / Gradle / Ivy

package com.crabshue.commons.xml;

import java.time.LocalDateTime;

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

import org.apache.commons.lang3.Validate;

import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.xml.exceptions.XmlErrorType;

/**
 * Utility class for XML date manipulations.
 *
 */
public class XmlDateUtils {

    /**
     * Provide {@code LocalDateTime.now()} as an {@link XMLGregorianCalendar}.
     *
     * @return the current moment as {@link XMLGregorianCalendar}.
     */
    public static XMLGregorianCalendar nowAsGregorian() {
        try {
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(LocalDateTime.now().toString());
        } catch (DatatypeConfigurationException e) {
            throw new SystemException(XmlErrorType.ISSUE_WITH_DATE_XML, e);
        }
    }

    /**
     * Convert a {@link LocalDateTime} to {@link XMLGregorianCalendar}.
     *
     * @param localDateTime the date time to convert.
     * @return the date time converted as XML Gregorian date.
     */
    public static XMLGregorianCalendar toXmlGregorian(final LocalDateTime localDateTime) {
        Validate.notNull(localDateTime);

        try {
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(localDateTime.toString());
        } catch (DatatypeConfigurationException e) {
            throw new SystemException(XmlErrorType.ISSUE_WITH_DATE_XML, e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy