users@jaxb.java.net

Re: Date subtypes

From: Joe Fialli <Joseph.Fialli_at_Sun.COM>
Date: Mon, 27 Jun 2005 16:56:18 -0400

Joe Fialli wrote:

> Dmitri Colebatch wrote:
>
>> Hi,
>>
>> I've got a type of the following:
>>
>> <xsd:simpleType name="BirthDate">
>> <xsd:restriction base="xsd:date">
>> <xsd:minInclusive value="1890-01-01"/>
>> <xsd:maxInclusive value="2049-12-31"/>
>> <xsd:pattern value="\d\d\d\d-\d\d-\d\d"/>
>> </xsd:restriction>
>> </xsd:simpleType>
>>
>>
> Unsure whether this inquiry is for schema to java or java to schema
> binding.
>
> JAXB 2.0 schema to java binding of the above binds to
> javax.xml.datatype.XMLGregorianCalendar.
> (See Table 6-1 in S 6.2.2. in JAXB 2.0 specification) That binding
> differentiates whether a
> field within a xsd:date was specifically set or undefined. For your
> example,
> it would remember that all fields not having to do with xs:date are
> not set
> (javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED) and they would
> not be marshalled out.
>
>> and then I have a snippet of code that is:
>>
>> @XmlElement(name = "BirthDate")
>> public Date getDob()
>> {
>> return dob;
>> }
>>
>> public void setDob(Date dob)
>> {
>> this.dob = dob;
>> }
>>
>> However this spits out
>>
>> <BirthDate>1975-06-15T00:00:00+10:00</BirthDate>
>>
>>
>
> For JAXB 2.0, Java to schema binds java.lang.Date to xsd:dateTime.
>
> For a short term work around, could you try XMLGregorianCalendar
> instead of Date.

I was incorrect for stating that this was a short term workaround.
I just double checked that the time component of Date is not optional
and there is no
way to represent that the time component does not exist in
java.lang.Date. All java.lang.Date
instances represent a specific millisec in time. XMLGregorianCalendar
uses FIELD_UNDEFINED to represent its fields that are not set,
getting the results that you desire.

-Joe

> This class was created to assist in preserving the XML calendar values
> more precisely
> than binding to java builtin calendar types.
>
> Following is way to create an instance in Java that behaves as you
> desire:
>
> import javax.xml.datatype.DatatypeFactory;
> import javax.xml.datatype.XMLGregorianCalendar;
>
> DatatypeFactory df = DatatypeFactory.newInstance();
> // create xsd:date instance 1975-06-15
> XMLGregorianCalendar dob =.df.newXMLGregorianCalendarDate(1975, 6, 15,
> javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED).
>
> If unmarshalling an xml document into XMLGregorianCalendar field, the
> class' constructor remembers which fields where set or not.
>
> Documented in the public review for the specification but not
> implemented in RI yet, the above
> field could be annotated with @XmlSchemaType(name="date") to map
> java.lang.Date to xsd:date.
> By default, JAXB 2.0 maps java.lang.Date to XMLGregorianCalendar.
>
> -Joe
>
>> which obviously isn't valid. I've been going well migrating across a
>> bunch of existing code but am a bit stumped on this one. Is there a
>> common solution to this?
>>
>> cheers
>> dim
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>