In this article, we will learn how to convert Instant to XMLGregorianCalendar and vice-versa in Java 1.8 version
1. Convert Instant to XMLGregorianCalendar :
- For conversion of Instant to XMLGregorianCalendar use DatatypeFactory
- DatatypeFactory.newInstance().newXMLGregorianCalendar(GregorianCalendar) returns XMLGregorianCalendar object
- First, convert Instant to ZonedDateTime using atZone() method passing ZoneId as input–argument
- Pass converted ZonedDateTime to GregorianCalendar.from() method which returns GregorianCalendar
- Pass converted GregorianCalendar to DatatypeFactory.newInstance().newXMLGregorianCalendar() method which returns XMLGregorianCalendar
- Converted XMLGregorianCalendar will have,
- Date & Time & Zone/Offset parts shifted to that particular Zone
- In short, Instant -> ZonedDateTime -> GregorianCalendar -> XMLGregorianCalendar
- Lets see an example for conversion of Instant to XMLGregorianCalendar in the below illustration
ConvertInstantToXmlGregorianCalendar.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import java.time.ZoneId;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class ConvertInstantToXmlGregorianCalendar {
public static void main(String[] args) throws DatatypeConfigurationException {
// 1. get instantaneous moment at UTC/GMT
Instant instant = Instant.now();
System.out.println("Current Instant at UTC/GMT is :- \n"
+ instant);
// 2. convert Instant -> GregorianCalendar
GregorianCalendar gregorianCalendar = GregorianCalendar
.from(instant.atZone(ZoneId.systemDefault()));
System.out.println("\nGregorianCalendar is :- \n"
+ gregorianCalendar.getTime());
// 3. convert GregorianCalendar to XMLGregorianCalendar
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
.newInstance()
.newXMLGregorianCalendar(
gregorianCalendar
);
// 4. print to console
System.out.print("\nConversion of Instant to XMLGregorianCalendar is :- \n"
+ xmlGregorianCalendar);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-21T05:57:59.565840600Z
GregorianCalendar is :-
Sun Aug 21 11:27:59 IST 2022
Conversion of Instant to XMLGregorianCalendar is :-
2022-08-21T11:27:59.565+05:30
2. Convert XMLGregorianCalendar to an Instant :
- First, create XMLGregorianCalendar object using DatatypeFactory.newInstance().newXMLGregorianCalendar(String) method
- For conversion of XMLGregorianCalendar to an Instant,
- First, convert XMLGregorianCalendar to GregorianCalendar using toGregorianCalendar() method
- And then invoke toInstant() method for conversion of GregorianCalendar to an Instant
- Converted Instant will have,
- Date & Time & Zone/Offset parts represents current Instant date/time at GMT/UTC
- In short, XMLGregorianCalendar -> GregorianCalendar -> Instant
- Lets see an example for conversion of XMLGregorianCalendar to an Instant in the below illustration
ConvertXmlGregorianCalendarToInstant.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class ConvertXmlGregorianCalendarToInstant {
public static void main(String[] args) throws DatatypeConfigurationException {
// 1. get new instance of XMLGregorianCalendar
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
.newInstance()
.newXMLGregorianCalendar(
"2022-12-23T18:28:34.438+05:30"
);
System.out.println("XMLGregorianCalendar/Date is :- \n"
+ xmlGregorianCalendar);
// 2. convert XMLGregorianCalendar to Instant
Instant instant = xmlGregorianCalendar
.toGregorianCalendar() // GregorianCalendar
.toInstant(); // Instant
// 3. print to console
System.out.print("\nConversion of XMLGregorianCalendar to an Instant is :- \n"
+ instant);
}
}
Output:
XMLGregorianCalendar/Date is :-
2022-12-23T18:28:34.438+05:30
Conversion of XMLGregorianCalendar to an Instant is :-
2022-12-23T12:58:34.438Z
Related Articles:
- Java 8 – Instant with method details and examples
- Java 8 – How to get instantaneous moment at UTC/GMT using Instant ?
- Java 8 – How to form an Instant passing Seconds and Nanoseconds fields ?
- Java 8 – How to get Seconds and Nanoseconds from an Instant ?
- Java 8 – How to parse Instant in String form ?
- Java 8 – How to convert Instant to LocalDate ?
- Java 8 – How to convert Instant to LocalTime ?
- Java 8 – How to convert Instant to LocalDateTime ?
- Java 8 – How to convert Instant to ZonedDateTime ?
- Java 8 – How to convert Instant to an OffsetDateTime ?
- Java 8 – How to convert Instant to number of Seconds and vice-versa ?
- Java 8 – How to convert Instant to number of Milliseconds and vice-versa ?
- Java 8 – How to convert Instant to java.util.Date and vice-versa ?
- Java 8 – How to convert Instant to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert Instant to Calendar and vice-versa ?
- Java 8 – How to convert Instant to GregorianCalendar and vice-versa ?
- Java 8 – How to convert Instant to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an Instant in different ways ?
- Java 8 – How to add Second, Millisecond and Nanosecond to an Instant ?
- Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?
- Java 8 – How to check whether an Instant is Before another Instant ?
- Java 8 – How to check whether an Instant is After another Instant ?
- Java 8 – How to compare two Instant instances ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
- Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
- Java 9 – How to convert Instant to LocalTime using ofInstant() method ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html
- https://docs.oracle.com/javase/8/docs/api/javax/xml/datatype/XMLGregorianCalendar.html
- https://docs.oracle.com/javase/8/docs/api/javax/xml/datatype/DatatypeFactory.html
Happy Coding !!
Happy Learning !!