Jump to: navigation, search

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

m (Replacing page with 'Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/special_schema_types001.htm')
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/special_schema_types001.htm
|info=y
+
|toc=y
+
|eclipselink=y
+
|eclipselinktype=MOXy
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlSchemaType.html XmlSchemaType]
+
}}
+
 
+
=Mapping Dates and Times=
+
 
+
You can use the '''@XmlSchemaType''' annotation to customize the XML representation of date and time information.  Additionally, EclipseLink MOXy supports the following types which are not covered in the JAXB specification (JSR-222):
+
 
+
* '''java.sql.Date'''
+
* '''java.sql.Time'''
+
* '''java.sql.Timestamp'''
+
 
+
 
+
==Example==
+
 
+
The following XML schema contains a '''date-of-birth''' element of type '''xsd:date''':
+
 
+
<source lang="xml">
+
<?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>
+
</source>
+
 
+
 
+
==Generated Model==
+
 
+
The JAXB XML Schema to Java compiler (XJC) can be used to generate a class model from the sample schema. For example:
+
 
+
<tt>
+
> xjc -d output-dir -p example date.xsd
+
</tt>
+
 
+
will generate the following '''Customer''' class:
+
 
+
<source lang="java">
+
package example;
+
+
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;
+
    }
+
+
}
+
</source>
+
 
+
Notice that:
+
 
+
* The '''dateOfBirth'''  property is of type '''javax.xml.datatype.XMLGregorianCalendar'''
+
* The '''dateOfBirth'''  property uses the '''@XmlSchemaType''' annotation
+
 
+
 
+
Some Java data types (like '''XMLGregorianCalendar''') have multiple XML representations (like '''xsd:date''', '''xsd:time''' or '''xsd:dateTime'''). Use '''@XmlSchemaType''' to select the appropriate representation.
+
 
+
 
+
==Using a Different Date (or Calendar) Property ==
+
By default, the JAXB XML schema to Java compiler (XJC) generates a property of type '''XMLGregorianCalendar'''. However, you can easily change his to '''java.util.Date''' or '''java.util.Calendar''', as shown here:
+
 
+
<source lang="java">
+
+
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;
+
    }
+
+
}
+
</source>
+

Latest revision as of 10:19, 8 November 2012

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