Date

I have a form with 3 fields, one for the day, one for the month and the other one for the year.

With this 3 fields I create a java.sql.Date object.

The question is. How can I make sure that the date is valid? For example, if in the field for the day the user enters 56, the date should be considered invalid, the same as if in the field month the user enters 13. Another case would be if the user enter 31/4/2000.

Can anybody tell me if there is a class or something that allows me to validate the date or should I program it

[551 byte] By [DesarrolloScreenNamea] at [2007-10-2 5:47:04]
# 1
When you convert a day,month,year into a date, it will normalise invalid dates.e.g. 30 Feb becomes 2 Mar.So all you need to do is to convert intoa Date and then get back the day/month/year and see if they are the same. If not the date was not valid.
Peter-Lawreya at 2007-7-16 1:56:45 > top of Java-index,Java Essentials,New To Java...
# 2

java.util.Calendar and java.text.SimpleDateFormat could both be useful.

The setLenient(false) method prevents it from being "nice" about the input dates.

something like this (untested);

// retrieve the input data

String day = getDay();

String month = getMonth();

String year = getYear();

// put it into a string and parse it

String inputDate = day + "/" + month + "/" + year;

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

sdf.setLenient(false);

Date date = null;

try{

date = sdf.parse(inputDate);

}

catch(ParseException e){

// deal with the error in some way

// return an error message to the user...

}

Cheers,

evnafets

evnafetsa at 2007-7-16 1:56:45 > top of Java-index,Java Essentials,New To Java...