String date="12/20/20102";
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date today;
try {
today = df.parse(date.trim());
Log.e("Error", "Correct");
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
Log.e("Error", "InCorrect");
}
I am not able to get this corrected. I always get this converted and if I give an incorrect date string also it not catching in the exception.
Please help me.
Your string input is :
Two mistakes:
String date = “12/20/20122“; // it shoud be “12/20/2012“
Your String input is in MM/dd/yyyy format and you trying to parse in dd/MM/yyyy format. Change your string input format (20/12/2012) OR change formatter to (MM/dd/yyyy).
EDIT
Here is sample code for validating user input date in dd/MM/yyyy format.
Note: Leap year and February month logic is not implemented.
Full validation with all conditions available here.
http://www.mkyong.com/regular-expressions/how-to-validate-date-with-regular-expression/
And don’t forget to accept this if it helps you.
Thanks…