In my android application have 2 edit text field those are taking from the date and last date, I am trying to check whether the date is valid or invalid I tried using this code but it is not working.. where is the error i cant find please help me to correct the code
String fromdate,todate;
private Matcher matcher;
private static final String DATE_PATTERN =
"(0?[1-9]|1[012]) [/.-] (0?[1-9]|[12][0-9]|3[01]) [/.-] ((19|20)\\d\\d)";
search button Onclick code
advsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fromdate=fromed.getText().toString();
todate=toed.getText().toString();
matcher = Pattern.compile(DATE_PATTERN).matcher(fromdate);
matcher = Pattern.compile(DATE_PATTERN).matcher(todate);
if (!matcher.matches()) {
Toast.makeText(getApplicationContext(), "Invalid Date!", Toast.LENGTH_LONG).show();
}
}
});
}
boolean class
public boolean validate(String date){
matcher = pattern.matcher(date);
if(matcher.matches()){
matcher.reset();
if(matcher.find()){
String day = matcher.group(1);
String month = matcher.group(2);
int year = Integer.parseInt(matcher.group(3));
if (day.equals("31") &&
(month.equals("4") || month .equals("6") || month.equals("9") ||
month.equals("11") || month.equals("04") || month .equals("06") ||
month.equals("09"))) {
return false; // only 1,3,5,7,8,10,12 has 31 days
} else if (month.equals("2") || month.equals("02")) {
//leap year
if(year % 4==0){
if(day.equals("30") || day.equals("31")){
return false;
}else{
return true;
}
}else{
if(day.equals("29")||day.equals("30")||day.equals("31")){
return false;
}else{
return true;
}
}
}else{
return true;
}
}else{
return false;
}
}else{
return false;
}
}
}
}