Validating the given date against the format dd/mm/yyyy.
Valid = 1
Not Valid = -1
Example1:
Input= 12/06/1987
output=1
Example2:
Input= 03/1/1987
output=-1
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CheckDateFormat {
public static void main(String[] args) {
String s1="29/02/2006";
getvalues(s1);
}
public static void getvalues(String s1) {
if(s1.matches("[0-9]{2}[/][0-9]{2}[/][0-9]{4}"))
{
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
try {
Date d1=sdf.parse(s1);
System.out.println(1);
} catch (ParseException e) {
//e.printStackTrace();//Always going to catch block
System.out.println(-1);
}
}
else
System.out.println(-1);
}
}
The problem with the code is that it it always returns -1.
It always enters the catch block and gives a ParseException.
Is there any problem with the Regex?