How To Convert Xmlgregoriancalendar To Appointment To Xmlgregoriancalendar Inwards Coffee - Event Tutorial

There are several ways to convert XMLGregorianCalendar to Date inwards Java. You tin terminate 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 practice XML documents from Java Objects in addition to Java objects from XML files. JAXB besides helps to practice Java classes from XML Schema file (.XSD file). By default JAXB maps XSD information type xs:date, xs:time in addition to xs:dateTime to XMLGregorianCalendar inwards Java, only yous tin terminate configure XJC to practice java.util.Date objects instead of javax.xml.datatype.XMLGregorianCalendar. Since java.util.Date is most pop agency of dealing amongst engagement in addition to fourth dimension inwards Java, nosotros oft ask to convert XMLGregorianCalendar representative to Date representative inwards Java. Thankfully past times using Java API, nosotros tin terminate easily practice this conversion of XMLGregorianCalendar to Date in addition to Date to XMLGregorianCalendar inwards Java. By the way, It's skillful to recall that XML Schema has 3 dissimilar types which tin terminate stand upwards for either date, fourth dimension or both, acre java.util.Date contains engagement in addition to fourth dimension information together. In this Java in addition to XML tutorial, nosotros volition run into representative of converting XMLGregorianCalendar to Date in addition to inverse inwards Java.

XMLGregorianCalendar to Date to XMLGregorianCalendar

Java program, which converts XMLGregorianCalendar instances to java.util.Date instances. We own got 2 method, ane which takes XMLGregorianCalendar in addition to provide java.util.Date, in addition to other which takes Date in addition to provide 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 in addition to inverse i.e. java.util.Date
 * to XMLGregorianCalendar. If yous are using XJC to practice Java classes from XML Schema
 * or XSD file, By default JAXB map XSD information types xs:date, xs:time in addition to xs:dateTime
 * to XMLGregorianCalendar inwards Java.
 *
 * @author Javin Paul
 */
public class XMLCalendarToDate {

    public static void main(String args[]) {
       Date today = new Date();
     
       //Converting engagement 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 engagement = 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 acre converting XMLGregorianCalendar to Date inwards Java.


1) XML Schema has dissimilar information type for date, time in addition to dateTime e.g. xsd:date, xsd:time in addition to 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 in addition to xs:dateTime information types. I volition write almost that later, only yous tin terminate notwithstanding explores this option.

3) While creating representative of GregorianCalendar, its ameliorate to purpose constructor instead calling GregorianCalendar.getInstance() because it's like to Calendar.getInstance() in addition to tin terminate provide dissimilar type of Calendar based upon Locale settings e.g. BuddhistCalendar for Thai locale or JapaneseImperialCalendar for Japan. By using constructor, yous besides removes type casting because getInstance() provide representative of java.util.Calendar, in addition to prevents ClassCastException inwards Java.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services in addition to REST API amongst Spring Boot
formatted String to stand upwards for engagement in addition to fourth dimension inwards XML files in addition to later on convert String to Date inwards Java program.


0 Response to "How To Convert Xmlgregoriancalendar To Appointment To Xmlgregoriancalendar Inwards Coffee - Event Tutorial"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel