Merge pull request #6472 from antmordel/BAEL-2522

Adding XMLGregorianCalendar to LocalDate converters
This commit is contained in:
Eric Martin 2019-03-20 21:10:23 -05:00 committed by GitHub
commit fc82f53cb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 0 deletions

29
java-dates-2/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml
#jenv
.java-version

55
java-dates-2/pom.xml Normal file
View File

@ -0,0 +1,55 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>java-dates-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>java-dates-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>java-dates-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,36 @@
package com.baeldung.xmlgregoriancalendar;
import org.junit.Test;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.time.LocalDate;
import static org.assertj.core.api.Assertions.assertThat;
public class XmlGregorianCalendarConverterUnitTest {
@Test
public void fromLocalDateToXMLGregorianCalendar() throws DatatypeConfigurationException {
LocalDate localDate = LocalDate.of(2017, 4, 25);
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toString());
assertThat(xmlGregorianCalendar.getYear()).isEqualTo(localDate.getYear());
assertThat(xmlGregorianCalendar.getMonth()).isEqualTo(localDate.getMonthValue());
assertThat(xmlGregorianCalendar.getDay()).isEqualTo(localDate.getDayOfMonth());
assertThat(xmlGregorianCalendar.getTimezone()).isEqualTo(DatatypeConstants.FIELD_UNDEFINED);
}
@Test
public void fromXMLGregorianCalendarToLocalDate() throws DatatypeConfigurationException {
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar("2017-04-25");
LocalDate localDate = LocalDate.of(xmlGregorianCalendar.getYear(), xmlGregorianCalendar.getMonth(), xmlGregorianCalendar.getDay());
assertThat(localDate.getYear()).isEqualTo(xmlGregorianCalendar.getYear());
assertThat(localDate.getMonthValue()).isEqualTo(xmlGregorianCalendar.getMonth());
assertThat(localDate.getDayOfMonth()).isEqualTo(xmlGregorianCalendar.getDay());
}
}

View File

@ -440,6 +440,7 @@
<module>java-collections-conversions</module> <module>java-collections-conversions</module>
<module>java-collections-maps</module> <module>java-collections-maps</module>
<!-- <module>java-dates</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 --> <!-- <module>java-dates</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
<!-- <module>java-dates-2</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
<!-- <module>java-ee-8-security-api</module> --> <!-- long running --> <!-- <module>java-ee-8-security-api</module> --> <!-- long running -->
<module>java-lite</module> <module>java-lite</module>
<module>java-numbers</module> <module>java-numbers</module>