Question

I am trying to change the format of XMLGregorianCalendar date. The code in my schema file(.xsd) is this:

    <xs:element name="LatestSaleDate">
    <xs:annotation>
        <xs:documentation>Latest sale date on the property (format MM/DD/YYYY)</xs:documentation>
    </xs:annotation>
    <xs:simpleType>
        <xs:restriction base="xs:date"/>
    </xs:simpleType>
</xs:element>

I created the Java classes by the XJC command

I got setter and getter to set latestsaledate as:

    @XmlElement(name = "LatestSaleDate")
    protected XMLGregorianCalendar latestSaleDate;

But when i trying to get date from db and assign to the XMLGregorianCalendar object it gives me IllegalArgumentException

Can anybody here help me how I can solve it and also format it so it only write to xml file in this format dd/MM/yyyy

Was it helpful?

Solution

Can anybody here help me how I can solve it and also format it so it only write to xml file in this format dd/MM/yyyy

You can't and shouldn't - at least not without changing the schema. Your schema expressly specifies that it's an xs:date - and the specified format of xs:date is basically ISO-8601 (yyyy-MM-dd, with an optional offset from UTC), not dd/MM/yyyy.

I would strongly suggest that you store your data in the standardized representation - don't forget that a user should never have to see that; you can parse/format the date according to the user's culture, and then store it in ISO-8601 format. It's very important to differentiate between storage/communication formats and presentation formats.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top