Adding XMLGregorianCalendar to LocalDate converters

This commit is contained in:
Antonio Moreno 2019-03-06 07:14:23 +00:00
parent ba16cee334
commit 4376424e51
2 changed files with 73 additions and 0 deletions

View File

@ -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());
}
}

View File

@ -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);
}
}