Canonical way to find if a date is valid or not ?

F

foo

Hi all:

What's the best way to validate whether a date
entered by a user (say in a html form) is
valid or not ?

Suppose, in my java code I get:

02/15/2004 [dd/mm/yyyy]

This is valid

However, if 2004 is a leap year, then the following
will not be valid:

02/29/2004

java.text.Dateformat.parse(..) parses both dates
fine. I need to reject the second date.

Best regards,

--foo
 
A

anonymous

foo said:
Hi all:

What's the best way to validate whether a date
entered by a user (say in a html form) is valid or not ?

Suppose, in my java code I get:

02/15/2004 [dd/mm/yyyy]

This is valid

However, if 2004 is a leap year, then the following
will not be valid:

02/29/2004

java.text.Dateformat.parse(..) parses both dates
fine. I need to reject the second date.

Best regards,

--foo
One of my colleages is working on something like this right now. I
believe the general idea is to first use a Pattern to make sure the date
has been entered using 10 characters, then use those characters to
construct a Calendar object. I don't know how far he is, but the guess
is that the Calendar object will balk at getting impossible values.
 
G

Gordon Beaton

What's the best way to validate whether a date
entered by a user (say in a html form) is
valid or not ? [...]
java.text.Dateformat.parse(..) parses both dates
fine. I need to reject the second date.

See the API documentation for:

DateFormat.isLenient()
Calendar.isLenient()

/gordon
 
J

Jacob

foo said:
What's the best way to validate whether a date
entered by a user (say in a html form) is valid or not ?

Use DateFormat as you suggest, but set isLenient to false.

DateFormat is lenient by default so that "illegal" dates
are transformed to legal ones. 29/02/2003 becomes 01/03/2003
etc., a feature you don't want in your case.
 
F

foo

DateFormat is lenient by default so that "illegal" dates
are transformed to legal ones. 29/02/2003 becomes 01/03/2003
etc., a feature you don't want in your case.

Yup, that works ! Thanks.

-------------------my test code-------------------
import java.text.*;
import java.util.*;
import fc.util.*;

public class dateparse
{
public static void main(String[] args) throws Exception
{
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);

//lenient by default
if (myargs.flagExists("strict"))
df.setLenient(false);

Date date = df.parse(args[0]);
System.out.println("parsed: " + date);

System.out.println("setting date parsing to NON lenient");
df.setLenient(false);

date = df.parse(args[0]);
System.out.println("parsed: " + date);
}
}
---------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top