gpt4 book ai didi

android - 日期验证不起作用

转载 作者:行者123 更新时间:2023-11-29 02:30:12 24 4
gpt4 key购买 nike

在我的 android 应用程序中有 2 个编辑文本字段,它们取自日期和最后一个日期,我正在尝试检查日期是否有效我尝试使用此代码但它不起作用..错误在哪里我找不到请帮我更正代码

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)";

搜索按钮Onclick代码

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();
}
}
});

}

bool 类

 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;
}
}
}
}

最佳答案

像下面这样为数据验证创建单独的类..

public class DateValidator {

private Pattern pattern;
private Matcher matcher;

private static final String DATE_PATTERN =
"(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";

public DateValidator() {
pattern = Pattern.compile(DATE_PATTERN);
}

/**
* Validate date format with regular expression
*
* @param date date address for validation
* @return true valid date fromat, false invalid date format
*/
public boolean validate(final 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;
}
}

然后在点击按钮后将其放在代码下方..

 fromdate = et.getText().toString();
todate = et2.getText().toString();
DateValidator dateValidator=new DateValidator();
if (dateValidator.validate(fromdate)){
Toast.makeText(getApplicationContext(),"Valid",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"InValid",Toast.LENGTH_SHORT).show();

}

和edittext 输入数据格式,如dd/mm/yyyy。

有关格式的更多信息,请参阅此链接 http://www.mkyong.com/regular-expressions/how-to-validate-date-with-regular-expression/

关于android - 日期验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49915025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com