if a user inputs a date in the text field witht the wrong format..how i can i make an error message that will catch the error that the input date format is wrong?
please answer me..asap
It depends. How are you going to be passing the validations. Assuming it's from a jsp, this validation could be done via javascript at the user interface - webpage.
Else, if u are to pass it strait to java, just surround the Date instantating line with a try catch block. After this you juss have to work on the program flow and java will catch the exception for you. It's up to you to ignore the date creation line or prompt the user for a valid date from there. For example -:
-
//BALALALALA CODE
-
// CODE TO RECEIVE THE USER date INPUT
-
String dateReceived = req.getParameter("myDate");
-
java.sql.Date progDate = null;
-
try{
-
progDate = java.sql.Date.valueOf(dateReceived);
-
}catch(DateFormatException dtfe){
-
//code for program flow after an invalid date is receved
-
}
-
If your date format is more persnalised and not the default ones of sql.Date, or util.Date, I suggest that you visit Regular Expressons to do the validation checks then.