In this article, we will learn how to convert ZonedDateTime to XMLGregorianCalendar in Java 1.8 version and vice-versa
1. Convert ZonedDateTime to XMLGregorianCalendar :
- For conversion of ZonedDateTime to XMLGregorianCalendar use DatatypeFactory
- DatatypeFactory.newInstance().newXMLGregorianCalendar(GregorianCalendar) returns XMLGregorianCalendar object
- First, convert ZonedDateTime to GregorianCalendar using GregorianCalendar.from() method which returns GregorianCalendar
- Pass converted GregorianCalendar to DatatypeFactory.newInstance().newXMLGregorianCalendar() method which returns XMLGregorianCalendar
- In short, ZonedDateTime -> GregorianCalendar -> XMLGregorianCalendar
- Lets see an example for conversion of ZonedDateTime to XMLGregorianCalendar in the below illustration
ConvertZonedDateTimeToXmlGregorianCalendar.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZonedDateTime;
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 ConvertZonedDateTimeToXmlGregorianCalendar {
public static void main(String[] args) throws DatatypeConfigurationException {
// 1. get Zoned date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- \n" + zonedDateTime);
// 2. get system default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("\nDefault System Zone is :- \n"
+ zoneId);
// 3. convert ZonedDateTime -> GregorianCalendar
GregorianCalendar gregorianCalendar = GregorianCalendar.from(zonedDateTime);
System.out.println("\nGregorianCalendar is :- \n"
+ gregorianCalendar.getTime());
// 4. convert GregorianCalendar to XMLGregorianCalendar
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
.newInstance()
.newXMLGregorianCalendar(
gregorianCalendar
);
// 5. print to console
System.out.print("\nConversion of ZonedDateTime to XMLGregorianCalendar is :- \n"
+ xmlGregorianCalendar);
}
}
Output:
Zoned Date/time is :-
2022-08-13T17:36:21.505137700+05:30[Asia/Calcutta]
Default System Zone is :-
Asia/Calcutta
GregorianCalendar is :-
Sat Aug 13 17:36:21 IST 2022
Conversion of ZonedDateTime to XMLGregorianCalendar is :-
2022-08-13T17:36:21.505+05:30
2. Convert XMLGregorianCalendar to ZonedDateTime :
- First, create XMLGregorianCalendar object using DatatypeFactory.newInstance().newXMLGregorianCalendar(String) method
- For conversion of XMLGregorianCalendar to ZonedDateTime,
- First, convert XMLGregorianCalendar to GregorianCalendar using toGregorianCalendar() method
- And then invoke toZonedDateTime() method to convert GregorianCalendar to ZonedDateTime in (yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[GMT+HH:mm]) format without zone information
- In short, XMLGregorianCalendar -> GregorianCalendar -> ZonedDateTime
- Note: for XMLGregorianCalendar to ZonedDateTime conversion with zone information use ZonedDateTime.of() method and pass LocalDateTime & ZoneId as input-arguments
- Lets see an example for conversion of XMLGregorianCalendar to ZonedDateTime in the below illustration
ConvertXmlGregorianCalendarToZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class ConvertXmlGregorianCalendarToZonedDateTime {
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 ZonedDateTime 1
ZonedDateTime zonedDateTime = xmlGregorianCalendar
.toGregorianCalendar() // GregorianCalendar
.toZonedDateTime(); // ZonedDateTime
// 2.1 print to console
System.out.println("\nConversion of XMLGregorianCalendar to ZonedDateTime[Offset] is :- \n"
+ zonedDateTime);
// 3. convert XMLGregorianCalendar to ZonedDateTime 2
ZonedDateTime zonedDateTime2 = ZonedDateTime.of(
zonedDateTime.toLocalDateTime(), // LocalDateTime
ZoneId.systemDefault() // ZoneId
);
// 3.1 print to console
System.out.print("\nConversion of XMLGregorianCalendar to ZonedDateTime[Zone] is :- \n"
+ zonedDateTime2);
}
}
Output:
XMLGregorianCalendar/Date is :-
2022-12-23T18:28:34.438+05:30
Conversion of XMLGregorianCalendar to ZonedDateTime[Offset] is :-
2022-12-23T18:28:34.438+05:30[GMT+05:30]
Conversion of XMLGregorianCalendar to ZonedDateTime[Zone] is :-
2022-12-23T18:28:34.438+05:30[Asia/Calcutta]
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/javax/xml/datatype/DatatypeFactory.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
Happy Coding !!
Happy Learning !!