com.crabshue.commons.xml.XmlDateUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-xml Show documentation
Show all versions of commons-xml Show documentation
Library for XML documents operations.
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);
}
}
}