在android中我正在尝试添加验证,但它不起作用我想要验证到日期。以下是日期代码。
我的数据格式是= MM / dd / yyyy
private static final String DATE_PATTERN =
"(0?[1-9]|[12][0-9]|3[01]) [/.-] (0?[1-9]|1[012]) [/.-] ((19|20)\\d\\d)";
String getdatefor = fromDateEtxt.getText().toString();
String datefors = toDateEtxt.getText().toString();
matcher = Pattern.compile(DATE_PATTERN).matcher(getdatefor);
matchers = Pattern.compile(DATE_PATTERN).matcher(datefors);
if(matcher.matches()&&matchers.matches()){
new AsyncLoadSales().execute(getdatefor, datefors);
}
//Birthday validator
else if (!matcher.matches()){
fromDateEtxt.setError("Please Enter Valid Date");
toDateEtxt.setError("Please Enter Valid Date");
}
else{
fromDateEtxt.setError("Please Enter Valid Date");
toDateEtxt.setError("Please Enter Valid Date");
}
答案 0 :(得分:0)
您可以在不使用模式的情况下尝试此操作。
private static final String DATE_PATTERN =
"(^(((0[1-9]|1[0-9]|2[0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)";
String getdatefor = fromDateEtxt.getText().toString();
String datefors = toDateEtxt.getText().toString();
if(getdatefor.matches(DATE_PATTERN)&&datefors.matches(DATE_PATTERN)){
new AsyncLoadSales().execute(getdatefor, datefors);
}
//Birthday validator
else if (!getdatefor.matches(DATE_PATTERN)){
fromDateEtxt.setError("Please Enter Valid Date");
toDateEtxt.setError("Please Enter Valid Date");
}
else{
fromDateEtxt.setError("Please Enter Valid Date");
toDateEtxt.setError("Please Enter Valid Date");
}