Showing posts with label JAXB Date. Show all posts
Showing posts with label JAXB Date. Show all posts

Wednesday, May 24, 2023

JAXB Date Format Example using Annotation | Java Date to XML DateTime String Conversion

To specify a specific date format using JAXB annotations when converting Java Date objects to XML datetime strings, you can use the @XmlSchemaType annotation along with the name and type attributes. 

Here's an example:


import javax.xml.bind.annotation.*;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import java.util.Date;

@XmlRootElement
public class Example {
    private Date date;

    public Example() {
    }

    public Example(Date date) {
        this.date = date;
    }

    @XmlElement
    @XmlSchemaType(name = "dateTime")
    public XMLGregorianCalendar getDate() throws Exception {
        XMLGregorianCalendar xmlCalendar = null;
        if (date != null) {
            xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
            xmlCalendar.setTime(date.getHours(), date.getMinutes(), date.getSeconds());
            xmlCalendar.setYear(date.getYear() + 1900);
            xmlCalendar.setMonth(date.getMonth() + 1);
            xmlCalendar.setDay(date.getDate());
        }
        return xmlCalendar;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}



In this example, the Example class represents a JAXB-annotated class with a Date field. To specify the date format, we use the @XmlSchemaType annotation on the getDate() method. The name attribute is set to "dateTime", indicating that the XML representation of the date should follow the XML Schema dateTime type. 

 Note that the getDate() method returns an XMLGregorianCalendar object instead of Date. The conversion from Date to XMLGregorianCalendar is done within the method. 

We use the DatatypeFactory.newInstance().newXMLGregorianCalendar() method to create a new instance of XMLGregorianCalendar and set its values based on the Date object. 

 Here's an example of how to convert a Date object to an XML datetime string using JAXB and the above-mentioned Example class:


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.Date;

public class DateToXmlExample {
    public static void main(String[] args) {
        Date date = new Date();
        Example example = new Example(date);

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Example.class);
            Marshaller marshaller = jaxbContext.createMarshaller();

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            StringWriter stringWriter = new StringWriter();
            marshaller.marshal(example, stringWriter);

            String xml = stringWriter.toString();
            System.out.println(xml);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}


In this example, we create a new instance of Date and an instance of Example using the date object. We then create a JAXBContext and a Marshaller as usual. 

The Example object is marshalled to XML using the Marshaller.marshal() method, and the resulting XML is printed to the console. The XML representation of the date will follow the format specified by the XML Schema dateTime type, such as 2021-05-24T15:30:00.