In this article, we will learn how to convert LocalDate to XMLGregorianCalendar in Java 1.8 version and vice-versa
1. Convert LocalDate to XMLGregorianCalendar :
- For conversion of LocalDate to XMLGregorianCalendar use DatatypeFactory
- DatatypeFactory.newInstance().newXMLGregorianCalendar(String) returns XMLGregorianCalendar object
- Lets see an example for conversion of LocalDate to XMLGregorianCalendar in the below illustration
ConvertLocalDateToXmlGregorianCalendar.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class ConvertLocalDateToXmlGregorianCalendar {
public static void main(String[] args) throws DatatypeConfigurationException {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System LocalDate is :- \n" + localDate);
// 2. convert LocalDate to XMLGregorianCalendar
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
.newInstance()
.newXMLGregorianCalendar(
localDate.toString()
);
// 3. print to console
System.out.print("\nConversion of LocalDate to XMLGregorianCalendar is :- \n"
+ xmlGregorianCalendar);
}
}
Output:
Current System LocalDate is :-
2022-08-02
Conversion of LocalDate to XMLGregorianCalendar is :-
2022-08-02
2. Convert XMLGregorianCalendar to LocalDate :
- First, create XMLGregorianCalendar object using DatatypeFactory.newInstance().newXMLGregorianCalendar(String) method
- For conversion of XMLGregorianCalendar to LocalDate, use one of the variants of LocalDate.of() method passing Year/Month/DayOfMonth values extracted from newly created XMLGregorianCalendar object
- Lets see an example for conversion of XMLGregorianCalendar to LocalDate in the below illustration
ConvertXmlGregorianCalendarToLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class ConvertXmlGregorianCalendarToLocalDate {
public static void main(String[] args) throws DatatypeConfigurationException {
// 1. get new instance of XMLGregorianCalendar
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
.newInstance()
.newXMLGregorianCalendar(
"2021-11-23"
);
System.out.println("XMLGregorianCalendar/Date is :- \n" + xmlGregorianCalendar);
// 2. convert XMLGregorianCalendar to LocalDate
LocalDate localDate = LocalDate.of(
xmlGregorianCalendar.getYear(),
xmlGregorianCalendar.getMonth(),
xmlGregorianCalendar.getDay()
);
// 3. print to console
System.out.print("\nConversion of XMLGregorianCalendar to LocalDate is :- \n"
+ localDate);
}
}
Output:
XMLGregorianCalendar/Date is :-
2021-11-23
Conversion of XMLGregorianCalendar to LocalDate is :-
2021-11-23
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/javax/xml/datatype/DatatypeFactory.html
- https://docs.oracle.com/javase/8/docs/api/javax/xml/datatype/XMLGregorianCalendar.html
Happy Coding !!
Happy Learning !!