diff --git a/java-dates/src/main/java/com/baeldung/xmlgregoriancalendar/XmlGregorianCalendarConverter.java b/java-dates/src/main/java/com/baeldung/xmlgregoriancalendar/XmlGregorianCalendarConverter.java new file mode 100644 index 0000000000..7f7515ec4b --- /dev/null +++ b/java-dates/src/main/java/com/baeldung/xmlgregoriancalendar/XmlGregorianCalendarConverter.java @@ -0,0 +1,29 @@ +package com.baeldung.xmlgregoriancalendar; + +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; +import java.time.LocalDate; + +public class XmlGregorianCalendarConverter { + + public static void main(String[] args) throws DatatypeConfigurationException { + LocalDate localDate = LocalDate.now(); + System.out.println("localdate: " + localDate); + XMLGregorianCalendar xmlGregorianCalendar = fromLocalDate(localDate); + System.out.println("xmlGregorianCalendar: " + xmlGregorianCalendar); + + xmlGregorianCalendar.setTime(1, 1, 30); + System.out.println("xmlGregorianCalendar with time information: " + xmlGregorianCalendar); + LocalDate newLocalDate = fromXMLGregorianCalendar(xmlGregorianCalendar); + System.out.println("newLocalDate: " + newLocalDate); + } + + static XMLGregorianCalendar fromLocalDate(LocalDate localDate) throws DatatypeConfigurationException { + return DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toString()); + } + + static LocalDate fromXMLGregorianCalendar(XMLGregorianCalendar xmlGregorianCalendar) { + return LocalDate.of(xmlGregorianCalendar.getYear(), xmlGregorianCalendar.getMonth(), xmlGregorianCalendar.getDay()); + } +} diff --git a/java-dates/src/test/java/com/baeldung/xmlgregoriancalendar/XmlGregorianCalendarConverterTest.java b/java-dates/src/test/java/com/baeldung/xmlgregoriancalendar/XmlGregorianCalendarConverterTest.java new file mode 100644 index 0000000000..f27c91be7e --- /dev/null +++ b/java-dates/src/test/java/com/baeldung/xmlgregoriancalendar/XmlGregorianCalendarConverterTest.java @@ -0,0 +1,44 @@ +package com.baeldung.xmlgregoriancalendar; + +import org.junit.Test; + +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; +import java.time.LocalDate; + +import static com.baeldung.xmlgregoriancalendar.XmlGregorianCalendarConverter.fromLocalDate; +import static com.baeldung.xmlgregoriancalendar.XmlGregorianCalendarConverter.fromXMLGregorianCalendar; +import static org.assertj.core.api.Assertions.assertThat; + +public class XmlGregorianCalendarConverterTest { + + @Test + public void fromLocalDateToXMLGregorianCalendar() throws DatatypeConfigurationException { + LocalDate localDate = LocalDate.of(2017, 4, 25); + XMLGregorianCalendar xmlGregorianCalendar = fromLocalDate(localDate); + + assertThat(xmlGregorianCalendar.getYear()).isEqualTo(localDate.getYear()); + assertThat(xmlGregorianCalendar.getMonth()).isEqualTo(localDate.getMonthValue()); + assertThat(xmlGregorianCalendar.getDay()).isEqualTo(localDate.getDayOfMonth()); + } + + @Test + public void fromXMLGregorianCalendarToLocalDate() throws DatatypeConfigurationException { + XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar("2017-04-25"); + LocalDate localDate = fromXMLGregorianCalendar(xmlGregorianCalendar); + + assertThat(localDate.getYear()).isEqualTo(xmlGregorianCalendar.getYear()); + assertThat(localDate.getMonthValue()).isEqualTo(xmlGregorianCalendar.getMonth()); + assertThat(localDate.getDayOfMonth()).isEqualTo(xmlGregorianCalendar.getDay()); + } + + @Test + public void compositionOfFunctionsIsIdentity() throws DatatypeConfigurationException { // Only if we don't consider time + LocalDate localDate = LocalDate.of(2017, 4, 25); + XMLGregorianCalendar xmlGregorianCalendar = fromLocalDate(localDate); + LocalDate resultDate = fromXMLGregorianCalendar(xmlGregorianCalendar); + + assertThat(resultDate).isEqualTo(localDate); + } +}