Re: html:text for Date property
I don't think most people get annoyed that the Struts UI can't deal with Dates. They get mad because BeanUtils can't! BeanUtils.copyProperties ignores Dates (for obvious reasons). Anyone using BeanUtils will have to deal with those properties themselves, and grumble the entire time. Once I tried LocaleBeanUtils.copyProperties(), but it's copying of Dates into Strings managed to include the time portion...which, well. I think you can see where I'm going. I'd ideally like to specify a DateFormat for some copyProperties to use. So, does someone have a good tutorial on how to get LocaleBeanUtils to do what we all really want it to do? After my experience with JSF, I had half a mind to subclass copyProperties to take a DateFormat parameter and go on. Then I found that none of the mirror sites had the Commons source for some reason. What's the expression? "Some days it doesn't pay to chew through the leather straps." -Joe > -Original Message- > From: Richard Yee [mailto:[EMAIL PROTECTED] > Sent: Friday, April 09, 2004 8:15 PM > To: Struts Users Mailing List; [EMAIL PROTECTED] > Subject: Re: html:text for Date property > > > Paul, > You shouldn't keep an instance of SimpleDateFormat as > an instance variable even though you are only using > the parse method. The reason is that the > SimpleDateFormat class is not thread-safe. This is > caused by the fact that the DateFormat class isn't > threadsafe due to the fact that a value (the date > portion of calendar) used only for the duration of a > single public method (parse) in is stored as a member > variable. (This design flaw was inspired by another > design flaw of a similar > nature in Calendar, but let's not go there.) > See: > http://developer.java.sun.com/developer/bugParade/bugs/4093418.html > http://developer.java.sun.com/developer/bugParade/bugs/4228335.html > http://developer.java.sun.com/developer/bugParade/bugs/4261469.html > > So to fix your code, you'd have to put it as a member > variable, thus incurring the overhead of the object > creation each time. > > This problem would be very hard to uncover if it went > into production. > > Regards, > > Richard > > --- Paul Barry <[EMAIL PROTECTED]> wrote: > > This seems to work pretty well: > > > > private Date dateOfBirth; > > private static final DateFormat dateOfBirthFormat = > > new SimpleDateFormat("MM/dd/"); > > > > 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 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
Re: html:text for Date property
Paul, You shouldn't keep an instance of SimpleDateFormat as an instance variable even though you are only using the parse method. The reason is that the SimpleDateFormat class is not thread-safe. This is caused by the fact that the DateFormat class isn't threadsafe due to the fact that a value (the date portion of calendar) used only for the duration of a single public method (parse) in is stored as a member variable. (This design flaw was inspired by another design flaw of a similar nature in Calendar, but let's not go there.) See: http://developer.java.sun.com/developer/bugParade/bugs/4093418.html http://developer.java.sun.com/developer/bugParade/bugs/4228335.html http://developer.java.sun.com/developer/bugParade/bugs/4261469.html So to fix your code, you'd have to put it as a member variable, thus incurring the overhead of the object creation each time. This problem would be very hard to uncover if it went into production. Regards, Richard --- Paul Barry <[EMAIL PROTECTED]> wrote: > This seems to work pretty well: > > private Date dateOfBirth; > private static final DateFormat dateOfBirthFormat = > new SimpleDateFormat("MM/dd/"); > > 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 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 > > > > 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/ > format instead? > Likewise, how > do I make it so the user can enter a date in the > format of > MM/DD/ 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] > __ Do you Yahoo!? Yahoo! Small Business $15K Web Design Giveaway http://promotions.yahoo.com/design_giveaway/
Re: html:text for Date property
This seems to work pretty well: private Date dateOfBirth; private static final DateFormat dateOfBirthFormat = new SimpleDateFormat("MM/dd/"); 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 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 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/ format instead? Likewise, how do I make it so the user can enter a date in the format of MM/DD/ 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]
Re: html:text for Date property
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 > >> > >> > >> > >>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/ format instead? > >>Likewise, how > >>do I make it so the user can enter a date in the format of > >> MM/DD/ 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]
RE: html:text for Date property
> From: Paul Barry [mailto:[EMAIL PROTECTED] > Are their other ways to handle this, so I don't need 2 properties? String properties in the Form, and a utility class to do the conversions when you need to use it as a Date. (Possibly a BeanUtils Converter to do it as part of 'copyProperties' if you have a Date in your business object?) Any time I've had the idea to use other than String, String[] or Boolean in an ActionForm, I've ended up changing back. Or separate fields for month/day/year to keep the users from messing it up. ;) Then you can validate on ranges of integers, and string the bits together to make a Date when you need one. Then again, I rarely need Dates... in my database, dates are stored as the number of integers since 12/31/1967. (I considered offering a prize to the first person to guess which one, but it's too easy to Google.) -- Wendy Smoak Application Systems Analyst, Sr. ASU IA Information Resources Management - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: html:text for Date property
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 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/ format instead? Likewise, how do I make it so the user can enter a date in the format of MM/DD/ 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]
RE: html:text for Date property
> ActionForm has a object that has a Date property that I want to set. > > So I have > > > > 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/ format instead? > Likewise, how > do I make it so the user can enter a date in the format of MM/DD/ > 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]