commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gabriel Belingueres (JIRA)" <>
Subject [jira] Commented: (VALIDATOR-221) DateValidator considers "02/29" with format "MM/dd" invalid
Date Tue, 30 Jan 2007 20:20:33 GMT


Gabriel Belingueres commented on VALIDATOR-221:

I think there is a subtle mistake here in trying to use a DateValidator to validate if "02/29"
is a valid date. The thing is that February the 29 is valid *depending on the year* you are
talking about, and this is a necessary context information that is lacking here.

As of why SimpleDateFormat assumes by default that you are talking about 1970 instead of throwing
an exception is beyond me understanding.

See this other example:

df = new SimpleDateFormat("dd");
d = df.parse("31");

Now d is 01/31/1970, so it is valid. Again SimpleDateFormat assumes month 1, year 1970.

Now I think that you should better of relaying in a regexp validator or some custom functionality
since what you are trying to validate definitively is not a date.

Perhaps DateValidator could detect those incomplete cases. May be it could check the pattern
parameter and try to ask if it include the following sub strings: d or dd or ddd, M or MM
or MMM, yy or yyyy to detect those invalid cases?

Anyway, as long as DateValidator is relying on SimpleDateFormat I see no simple solution here.

> DateValidator considers "02/29" with format "MM/dd" invalid
> -----------------------------------------------------------
>                 Key: VALIDATOR-221
>                 URL:
>             Project: Commons Validator
>          Issue Type: Bug
>    Affects Versions: 1.3.1 Release
>         Environment: Windows XP, Java 1.5.0_04
>            Reporter: Carsten Drossel
>            Priority: Minor
> When the date pattern contains only day and month the isValid(..)-method of DateValidator
returns false for the value Feb. 29th.
> Here is a JUnit test that fails:
> public void testFebruary29th() throws Exception {
>   assertTrue( DateValidator.getInstance().isValid( "02/29", "MM/dd", true ) );
> }
> The DateValidator uses the parse(..)-method of SimpleDateFormat for the validation. This
method appears to complete any date using 01/01/1970 00:00. Since 1970 was not a leap year
a ParseException is thrown for the input "02/29" with the format "MM/dd" because 02/29/1970
is not a valid date.
> But IMHO "02/29" should be valid. An example where it makes sense is a search for persons
with a certain birthday. 

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message