This seems to work pretty well:

private Date dateOfBirth;
private static final DateFormat dateOfBirthFormat =
    new SimpleDateFormat("MM/dd/yyyy");

public Date getDateOfBirth() {
    return dateOfBirth;
}

public void setDateOfBirth(Date dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

public String getDateOfBirthString() {
    return dateOfBirthFormat.format(dateOfBirth);
}
        
public void setDateOfBirthString(String dateOfBirthString)
    throws ParseException {
    this.dateOfBirth = dateOfBirthFormat.parse(dateOfBirthString);      
}

And then I use dateOfBirthString as the property in the <html:text> tag. I'll also create a rule in the validator to make sure that dateOfBirthString is the right format, so the ParseException never happens.

Christian Bollmeyer wrote:

On Friday 09 April 2004 21:19, Paul Barry wrote:

Generally, it's a good idea to have only String and boolean
properties in an ActionForm and convert the information
gathered for further processing lateron. For complex
validations (like Dates), I usually check in validate() if
the value entered can be successfully converted via
SimpleDateFormat and do the actual conversion when
populating the VO bean. But you can have 2 properties
in the form as well.


HTH,
-- Chris.

BTW, as such conversions are needed quite often,
it's a good idea to write a small utility function that
does the conversion check and put it in either
your BaseActionForm or some general utility class.


Yeah, I guess I could do that. I think need 2 properties. I would
create a dateAsString property, have the form get and set that, and
then have the getters and setters set and convert the actual Date. This way I can call getDate to get a Date and getDateAsString to get
it as a formatted String.


Are their other ways to handle this, so I don't need 2 properties?

Slattery, Tim - BLS wrote:

ActionForm has a object that has a Date property that I want to
set.

So I have

<html:text property="object.date">

If I populate the that property in the Action like this:

MyObject obj = new MyObject();
obj.setDate(new Date());
form.setObject(obj);

The html:text tag does a toString() on the object.date property. How can I get it to display in the MM/DD/YYYY format instead?
Likewise, how
do I make it so the user can enter a date in the format of
MM/DD/YYYY and have it correctly set the Date property?

I'd have the getter format the Date as a string (use SimpleDateFormat.format(), you can specify any of a wide variety of formats). Likewise, the setter should accept a string and use SimpleDateFormat.parse() to turn it into a Date. If parse() returns null then the String could not be turned into a Date, and you need to display an error message.


------------------------------------------------------------------- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to