Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Simple Values/Special Schema Types/Date and Time Types"

m (New page: JAXB and Date/Time Properties In this post I will describe how JAXB handles date/time information. This post will also cover how the @XmlSchemaType annotation can be used to customize the...)
 
m (Replacing page with 'Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/special_schema_types001.htm')
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
JAXB and Date/Time Properties
+
Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/special_schema_types001.htm
In this post I will describe how JAXB handles date/time information.  This post will also cover how the @XmlSchemaType annotation can be used to customize the XML representation.
+
 
+
XML Schema (date.xsd)
+
 
+
The following XML schema will be used for this post.  Note that the date-of-birth element is of type xsd:date.
+
 
+
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
+
<?xml version="1.0" encoding="UTF-8"?>
+
<xsd:schema
+
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
+
    <xsd:element name="customer">
+
        <xsd:complexType>
+
            <xsd:sequence>
+
                <xsd:element
+
                    name="date-of-birth"
+
                    type="xsd:date"
+
                    minOccurs="0"/>
+
            </xsd:sequence>
+
        </xsd:complexType>
+
    </xsd:element>
+
+
</xsd:schema>
+
 
+
Generated Model - XMLGregorianCalendar Property
+
 
+
 
+
From this XML schema we can generate a class model using the following XJC call:
+
 
+
1
+
+
xjc -d out -p blog.date date.xsd
+
 
+
This will result in a Customer class that looks like the following.  There are a couple of interesting things to note:
+
 
+
  1. The dateOfBirth property is of type javax.xml.datatype.XMLGregorianCalendar. 
+
  2. The use of @XmlSchemaType.
+
 
+
Some Java data types (like XMLGregorianCalendar) have multiple XML representations (like xsd:date, xsd:time, xsd:dateTime, etc.) and @XmlSchemaType is used to choose the desired representation.
+
 
+
 
+
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
+
package blog.date;
+
+
import javax.xml.bind.annotation.XmlAccessType;
+
import javax.xml.bind.annotation.XmlAccessorType;
+
import javax.xml.bind.annotation.XmlElement;
+
import javax.xml.bind.annotation.XmlRootElement;
+
import javax.xml.bind.annotation.XmlSchemaType;
+
import javax.xml.bind.annotation.XmlType;
+
import javax.xml.datatype.XMLGregorianCalendar;
+
+
@XmlAccessorType(XmlAccessType.FIELD)
+
@XmlType(name = "", propOrder = {"dateOfBirth"})
+
@XmlRootElement(name = "customer")
+
public class Customer {
+
+
    @XmlElement(name = "date-of-birth")
+
    @XmlSchemaType(name = "date")
+
    protected XMLGregorianCalendar dateOfBirth;
+
+
    public XMLGregorianCalendar getDateOfBirth() {
+
        return dateOfBirth;
+
    }
+
+
    public void setDateOfBirth(XMLGregorianCalendar value) {
+
        this.dateOfBirth = value;
+
    }
+
+
}
+
 
+
User Defined - Date (or Calendar) Property
+
 
+
Even though the JAXB XML schema to Java compiler (XJC) generates a property of type XMLGregorianCalendar by default, you are free to use java.util.Date and java.util.Calendar as well.
+
 
+
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
+
package blog.date;
+
+
import java.util.Date;
+
+
import javax.xml.bind.annotation.XmlAccessType;
+
import javax.xml.bind.annotation.XmlAccessorType;
+
import javax.xml.bind.annotation.XmlElement;
+
import javax.xml.bind.annotation.XmlRootElement;
+
import javax.xml.bind.annotation.XmlSchemaType;
+
+
@XmlAccessorType(XmlAccessType.FIELD)
+
@XmlRootElement(name = "customer")
+
public class Customer {
+
+
    @XmlElement(name = "date-of-birth")
+
    @XmlSchemaType(name = "date")
+
    protected Date dateOfBirth;
+
+
    public Date getDateOfBirth() {
+
        return dateOfBirth;
+
    }
+
+
    public void setDateOfBirth(Date value) {
+
        this.dateOfBirth = value;
+
    }
+
+
}
+
 
+
EclipseLink JAXB (MOXy) Extension
+
 
+
EclipseLink JAXB (MOXy) also supports the following types which are not covered in the JAXB specification (JSR-222):
+
 
+
    * java.sql.Date
+
    * java.sql.Time
+
    * java.sql.Timestamp
+

Latest revision as of 10:19, 8 November 2012

Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/special_schema_types001.htm

Back to the top