You determine the way you want dates to be formatted in a form by using the date
converter. The date
converter uses the formatting and parsing methods and formats of the java.text.SimpleDateFormat
class. For example, to format a date as November 12, 1998
, use date="MMM d, yyyy"
. If you want to format the same date as 11/12/98
, use date="M/dd/yy"
. See the API reference for java.text.SimpleDateFormat
in your JDK for full details of how the converter parses strings into Dates and formats Dates into strings for display.
Because the date
converter does not flag an error if a two-digit value is entered when a four-digit value is expected for the year, you might need to add error checking to your form validation logic to handle this case. Also, if the field expects a two-digit year entry, the year is interpreted as being no more than 80 years before or 20 years after the current date.
This example uses the date converter to format a date:
<dsp:input bean="NewUser.lastActivityDate" date="MMMM d, yyyy"/>
This example defines a parameter that is a Date, not just a String:
<dsp:param name="lastActivityDate" value="3/4/98" date="M/dd/yy"/>
If your form contains a date input field, you might want to place a note near that field instructing users to supply the date in the expected format. If a date is entered in the wrong format, it is interpreted according to the format specified in the date converter, and as a result might store an incorrect date.
mindate/maxdate
You can use the date
converter’s optional mindate
and maxdate
attributes individually or together to restrict the range of dates a field accepts. For example, if you have a field for the user’s birth date, you typically want to require that the user enter a four-digit year to make sure the date the customer enters is unambiguous, as in this tag:
<dsp:input bean="NewUser.birthDate" date="M/dd/yyyy"/>
If users enter a two-digit year instead, the date might be interpreted differently than expected. For example, 2/16/59 is interpreted as February 16, 0059. By using the mindate
attribute, however, you can reject dates that are earlier than a certain date you specify. For example, the following tag includes a mindate
(in the format specified by the date attribute) of January 1, 1900:
<dsp:input bean="Employee1.birthDate" date="M/dd/yyyy" mindate="01/01/1900"/>
In this case, if the user enters 2/16/59, the entry is rejected, because February 16, 0059 is earlier than the date specified by mindate
.
When a user enters a date that is outside of the bounds specified by mindate
and maxdate
, the converter rejects the value and throws a TagConversionException
with an invalidDate
error code. You can include error handling in your form to display a message instructing the user to enter a valid date.