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

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

There is a newer version: 1.1.0
Show newest version
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 com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.xml.exceptions.XmlErrorType;
import lombok.NonNull;

/**
 * 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(@NonNull final LocalDateTime 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