Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Date in Castor

Thread view: 
veerleverbr@hotmail.com - 21 Jun 2005 14:20 GMT
Hi,

I have an XML file with dates in it, formatted as:
<birthday>
  <day>21>/day>
  <month>06</month>
  <year>1975</year>
</birthday>

I have an xsd that describes this as:
<xs:complexType name="dateDef">
 <xs:sequence>
   <xs:element name="day" type="xs:integer" minOccurs="1"
maxOccurs="1"/>
   <xs:element name="month" type="xs:integer" minOccurs="1"
maxOccurs="1"/>
   <xs:element name="year" type="xs:integer" minOccurs="1"
maxOccurs="1"/>
 </xs:sequence>
</xs:complexType>

When running SourceGenerator, Castor creates a class DateDef.java.

What do I have to change to make Castor understand that this is
actually a date, and that he has to turn it into the type
java.util.Date and that he has to validate wether this a valid date?

Any help would be appreciated...
Veerle
Chris - 21 Jun 2005 15:52 GMT
Dates in castor are a little bit tricky. I'm not quite sure but I think
castor itself saves the values in some kind of string format yyyy-mm-dd

So I recommend to format your date as a string in xsd-file.
<xsd:element name="AccessDate" type="xsd:string"/>

If you read the string date values use this method:

public static void setAccessDate(String selValue) {
   Date calcDate;

   try {
       //Set input format of string date
       java.text.SimpleDateFormat sdfInput = new
java.text.SimpleDateFormat(
           "yyyy-MM-dd");

       //Convert date in string format to date format
       calcDate = sdfInput.parse(selValue);
     }
     catch (Exception e) {
       //Set date on today
       calcDate = (new Date());
     }
   }

   //Set value of input field
   datefieldAccessDate.setValue(calcDate);
 }

If you write back to castor, first convert date values in your application
to string values in this format. You could use the following function for
this task.

public static String getAccessDate() {
   //Convert date to string in format yyyy-MM-dd
   java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
       "yyyy-MM-dd");
   String convertedDate = sdf.format(datefieldAccessDate.getValue());

   //Return the converted date
   return convertedDate;
 }

Regards,
Chris

> Hi,
>
[quoted text clipped - 25 lines]
> Any help would be appreciated...
> Veerle
veerleverbr@hotmail.com - 28 Jun 2005 08:46 GMT
No, I found a much better solution:

In the xsd is specify my date elements as: <xsd:element name="..."
type="xs:dateTime"/>

When I then generate my Castor objects, Castor automatically takes
java.util.Date as the type for these variables.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.