You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by GABOREAU Véronique <VG...@FASSET.FR> on 2001/03/02 10:42:35 UTC

Date fields

Hi,

I have the following problem : in my JSP, I have a form containing some date
fields. The value of these date fields is then saved in a database under the
Sql Date format.

My problem is that, during the populating of the form bean, the
populate(...) method cannot convert input text into Date format.

Should I use the String type for dates in the form bean ? (but then I need
to convert them as Date before inserting in the database).

Does somebody have any idea how I could handle the dates ?

Thanks in advance for any advice.

Veronique Gaboreau.

Re: Date fields

Posted by John Raley <jo...@moonlight.com>.
You could use String as the property type, and have another method to retrieve
the data as a date for the database's use:

MyForm {
    private String dateStr;  // may not be valid date
    String getDateStr() { return dateStr;}
    String setDateStr(String value) {... /* validate with DateFormat
somewhere*/ }
    Timestamp getDate() { /* parse dateStr using DateFormat - throw exception
if invalid */ }
}

Don't store dates as Strings in the database - you and anyone else who uses the
database will regret it.


GABOREAU Véronique wrote:

> Hi,
>
> I have the following problem : in my JSP, I have a form containing some date
> fields. The value of these date fields is then saved in a database under the
> Sql Date format.
>
> My problem is that, during the populating of the form bean, the
> populate(...) method cannot convert input text into Date format.
>
> Should I use the String type for dates in the form bean ? (but then I need
> to convert them as Date before inserting in the database).
>
> Does somebody have any idea how I could handle the dates ?
>
> Thanks in advance for any advice.
>
> Veronique Gaboreau.


Re: Date fields

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
GABOREAU Véronique wrote:

> Hi,
>
> I have the following problem : in my JSP, I have a form containing some date
> fields. The value of these date fields is then saved in a database under the
> Sql Date format.
>
> My problem is that, during the populating of the form bean, the
> populate(...) method cannot convert input text into Date format.
>
> Should I use the String type for dates in the form bean ? (but then I need
> to convert them as Date before inserting in the database).
>
> Does somebody have any idea how I could handle the dates ?
>

For the moment, using Strings in your form beans is the best thing to do.  It
also lets you deal with input errors (such as invalid formats).

In future versions of Struts, there will be enhanced support for data type
conversions for classes like dates.

>
> Thanks in advance for any advice.
>
> Veronique Gaboreau.

Craig McClanahan