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 (Generated Model)
m (Generated Model)
Line 43: Line 43:
  
  
By using this XJC call, you can generate a class model from the sample schema. For example:
+
By using this JAXB XML schema to Java compiler (XJC) call, you can generate a class model from the sample schema. For example:
  
 
<tt>
 
<tt>
Line 87: Line 87:
 
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.
 
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:
  
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.
 
 
<source lang="java">
 
<source lang="java">
 
 
Line 121: Line 120:
 
}
 
}
 
</source>
 
</source>
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
 
  
  

Revision as of 13:40, 5 April 2011

EclipseLink MOXy

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Elug api package icon.png Key API

Date and Time Types

You can use the @XMLSchemaType annotation to customize the XML representation of date and time information.

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

Sample Schema

This example shows how JAXB handles date and time information. In this sample XML schema, notice that the date-of-birth element is of type xsd:date.

 
<?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


By using this JAXB XML schema to Java compiler (XJC) call, you can generate a class model from the sample schema. For example:

xjc -d out -p blog.date date.xsd

will generate the following Customer class:

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;
    }
 
}

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:

 
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-logo.gif
Version: 2.2.0 Draft
Other versions...

Copyright © Eclipse Foundation, Inc. All Rights Reserved.