How To Convert Xmlgregoriancalendar To Appointment To Xmlgregoriancalendar Inward Coffee - Event Tutorial
There are several ways to convert XMLGregorianCalendar to Date inwards Java. You tin sack convert XMLGregorianCalendar to either java.util.Date or java.sql.Date based upon your need. JAXB (Java API/Architecture for XML Bindings) is a pop framework to practise XML documents from Java Objects together with Java objects from XML files. JAXB too helps to practise Java classes from XML Schema file (.XSD file). By default JAXB maps XSD information type xs:date, xs:time together with xs:dateTime to XMLGregorianCalendar inwards Java, precisely you lot tin sack configure XJC to practise java.util.Date objects instead of javax.xml.datatype.XMLGregorianCalendar. Since java.util.Date is most pop agency of dealing amongst appointment together with fourth dimension inwards Java, nosotros oftentimes demand to convert XMLGregorianCalendar lawsuit to Date lawsuit inwards Java. Thankfully past times using Java API, nosotros tin sack easily practise this conversion of XMLGregorianCalendar to Date together with Date to XMLGregorianCalendar inwards Java. By the way, It's expert to recall that XML Schema has 3 unlike types which tin sack stand upwardly for either date, fourth dimension or both, piece java.util.Date contains appointment together with fourth dimension information together. In this Java together with XML tutorial, nosotros volition run into representative of converting XMLGregorianCalendar to Date together with inverse inwards Java.
XMLGregorianCalendar to Date to XMLGregorianCalendar
Java program, which converts XMLGregorianCalendar instances to java.util.Date instances. We possess got 2 method, i which takes XMLGregorianCalendar together with furnish java.util.Date, together with other which takes Date together with furnish XMLGregorianCalendar.
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
/**
* Java programme to convert XMLGregorianCalendar to Date together with inverse i.e. java.util.Date
* to XMLGregorianCalendar. If you lot are using XJC to practise Java classes from XML Schema
* or XSD file, By default JAXB map XSD information types xs:date, xs:time together with xs:dateTime
* to XMLGregorianCalendar inwards Java.
*
* @author Javin Paul
*/
public class XMLCalendarToDate {
public static void main(String args[]) {
Date today = new Date();
//Converting appointment to XMLGregorianCalendar inwards Java
XMLGregorianCalendar xml = toXMLGregorianCalendar(today);
System.out.println("XMLGregorianCalendar from Date inwards Java : " + xml) ;
//Converting XMLGregorianCalendar to java.util.Date inwards Java
Date appointment = toDate(xml);
System.out.println("java.util.Date from XMLGregorianCalendar inwards Java : " + date);
}
/*
* Converts java.util.Date to javax.xml.datatype.XMLGregorianCalendar
*/
public static XMLGregorianCalendar toXMLGregorianCalendar(Date date){
GregorianCalendar gCalendar = new GregorianCalendar();
gCalendar.setTime(date);
XMLGregorianCalendar xmlCalendar = null;
try {
xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCalendar);
} catch (DatatypeConfigurationException ex) {
Logger.getLogger(StringReplace.class.getName()).log(Level.SEVERE, null, ex);
}
return xmlCalendar;
}
/*
* Converts XMLGregorianCalendar to java.util.Date inwards Java
*/
public static Date toDate(XMLGregorianCalendar calendar){
if(calendar == null) {
return null;
}
return calendar.toGregorianCalendar().getTime();
}
}
Output:
XMLGregorianCalendar from Date inwards Java : 2013-01-27T23:45:23.322-04:30
java.util.Date from XMLGregorianCalendar inwards Java : Sun Jan 27 23:45:23 VET 2013
Important betoken to note:
Few points which is worth knowing piece converting XMLGregorianCalendar to Date inwards Java.
1) XML Schema has unlike information type for date, time together with dateTime e.g. xsd:date, xsd:time together with xsd:dateTime, By default JAXB XJC maps all these to XMLGregorianCalendar inwards Java.
2) It's possible to customize XJC to generate Date instead of XMLGregorianCalendar for xs:date, xs:time together with xs:dateTime information types. I volition write most that later, precisely you lot tin sack all the same explores this option.
3) While creating lawsuit of GregorianCalendar, its ameliorate to role constructor instead calling GregorianCalendar.getInstance() because it's like to Calendar.getInstance() together with tin sack furnish unlike type of Calendar based upon Locale settings e.g. BuddhistCalendar for Thai locale or JapaneseImperialCalendar for Japan. By using constructor, you lot too removes type casting because getInstance() furnish lawsuit of java.util.Calendar, together with prevents ClassCastException inwards Java.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services together with REST API amongst Spring Boot
formatted String to stand upwardly for appointment together with fourth dimension inwards XML files together with afterwards convert String to Date inwards Java program.
Komentar
Posting Komentar