In this article, we will learn how to convert LocalTime to XMLGregorianCalendar in Java 1.8 version and vice-versa
1. Convert LocalTime to XMLGregorianCalendar :
- For conversion of LocalTime to XMLGregorianCalendar use DatatypeFactory
- DatatypeFactory.newInstance().newXMLGregorianCalendar(String) returns XMLGregorianCalendar object
- Lets see an example for conversion of LocalTime to XMLGregorianCalendar in the below illustration
ConvertLocalTimeToXmlGregorianCalendar.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class ConvertLocalTimeToXmlGregorianCalendar {
public static void main(String[] args) throws DatatypeConfigurationException {
// 1. get current system time
LocalTime localTime = LocalTime.now();
System.out.println("Current system time is :- \n"
+ localTime);
// 2. convert LocalDate to XMLGregorianCalendar - lexical representation
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
.newInstance()
.newXMLGregorianCalendar(
localTime.toString()
);
// 3. print to console
System.out.print("\nConversion of LocalTime to XMLGregorianCalendar is :- \n"
+ xmlGregorianCalendar);
}
}
Output:
Current system time is :-
20:39:05.111487
Conversion of LocalTime to XMLGregorianCalendar is :-
20:39:05.111487
2. Convert XMLGregorianCalendar to LocalTime :
- First, create XMLGregorianCalendar object using DatatypeFactory.newInstance().newXMLGregorianCalendar(String) method
- For conversion of XMLGregorianCalendar to LocalTime, use one of the variants of LocalTime.of() method passing Hour/Minute/Second values extracted from newly created XMLGregorianCalendar object
- Lets see an example for conversion of XMLGregorianCalendar to LocalTime in the below illustration
ConvertXmlGregorianCalendarToLocalTime.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class ConvertXmlGregorianCalendarToLocalTime {
public static void main(String[] args) throws DatatypeConfigurationException {
// 1. get new instance of XMLGregorianCalendar
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
.newInstance()
.newXMLGregorianCalendar(
"10:48:24.062919400"
);
System.out.println("XMLGregorianCalendar time is :- \n" + xmlGregorianCalendar);
// 2. convert XMLGregorianCalendar to LocalTime
LocalTime localTime = LocalTime.of(
xmlGregorianCalendar.getHour(),
xmlGregorianCalendar.getMinute(),
xmlGregorianCalendar.getSecond()
);
// 3. print to console
System.out.print("\nConversion of XMLGregorianCalendar to LocalTime is :- \n"
+ localTime);
}
}
Output:
XMLGregorianCalendar time is :-
10:48:24.062919400
Conversion of XMLGregorianCalendar to LocalTime is :-
10:48:24
Related Articles:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.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 !!