如何在android中验证范围?

时间:2017-02-12 02:48:34

标签: java android validation date android-studio

我是Android编程的新手,我正在开发一款应用。有人可以帮助我如何验证用户输入的日期,如果它在7天的范围内? 所以,我有两个字符串'startDay'和'endDay'用户输入 startDay = 1/2/2017 endDay = 6/2/2017

然后它继续下一步。如果用户输入 startDay = 1/2/2017 endDay = 9/2/2017然后返回消息“最多7天”

我有代码

@Override
public void onClick(View v) {

    if (v==btnSearch){

        String startDaystr = startDay.getText().toString();
        String startMonthtstr = startMonth.getText().toString();
        String endDaystr = endDay.getText().toString();
        String endMonthstr = endMonth.getText().toString();
        String endYearstr = endYear.getText().toString();



        if(Integer.valueOf(startDaystr) >1 && Integer.valueOf(endDaystr) < 8){
            sharedPreferenceCustom = SharedPreferenceCustom.getInstance(getContext());
            sharedPreferenceCustom.putSharedPref("startDay", startDaystr);
            sharedPreferenceCustom.putSharedPref("startMonth",startMonthtstr);
            sharedPreferenceCustom.putSharedPref("endDay",endDaystr);
            sharedPreferenceCustom.putSharedPref("endMonth",endMonthstr);
            sharedPreferenceCustom.putSharedPref("endYear",endYearstr);
            startActivity(new Intent(getActivity(), activity_history.class));
        }else{
            Toast.makeText(getActivity(), "Maximum 7 days!", Toast.LENGTH_LONG).show();
        }



    }
}

但是当我输入 startDay = 22/2/2017 endDay = 25/2/2017

结果是'最多7天'它应该返回到下一步

请帮助我..我搜索并尝试过,但没有得到这样的解决方案..

2 个答案:

答案 0 :(得分:1)

我添加了startYearStr,请注意。 使用以下代码:

String startDaystr = startDay.getText().toString();   
String startMonthtstr = startMonth.getText().toString();
String startYearstr = startYear.getText().toString();

String endDaystr = endDay.getText().toString();
String endMonthstr = endMonth.getText().toString();
String endYearstr = endYear.getText().toString();


Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(startDaystr));
startDate.set(Calendar.MONTH, Integer.parseInt(startMonthtstr));
startDate.set(Calendar.YEAR, Integer.parseInt(startYearstr));
startDate.set(Calendar.HOUR_OF_DAY, 0);
startDate.set(Calendar.MINUTE, 0);
startDate.set(Calendar.SECOND, 0);

Calendar endDate = Calendar.getInstance();
endDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(endDaystr));
endDate.set(Calendar.MONTH, Integer.parseInt(endMonthstr));
endDate.set(Calendar.YEAR, Integer.parseInt(endYearstr));
endDate.set(Calendar.HOUR_OF_DAY, 0);
endDate.set(Calendar.MINUTE, 0);
endDate.set(Calendar.SECOND, 0);

long diff = endDate.getTimeInMillis() - startDate.getTimeInMillis();
long dayCount =  diff / (24 * 60 * 60 * 1000);

dayCount为您提供两个日期之间的日差异。

答案 1 :(得分:0)

要比较您希望使用Java var answers = [ { color: 'green', text: "Signs point to yes." }, { color: 'green', text: "Yes." }, ... ]; function response() { var randnum = Math.floor((Math.random() * answers.length)); //8-Ball Icosahedron Answers document.getElementById("response").setAttribute("class", answers[randnum].color); document.getElementById("response").innerHTML = answers[randnum].text; } Date对象的日期,但是从EditText中读取初始日期,就像您正在做的那样不安全。如果您没有验证输入,用户可以输入类似于&#34; 2016年2月2日&#34;当你期待&#34; 2/2 / 2016&#34;因此您可能希望将其从自由格式的EditText更改为日期选择器,以便您可以控制用户首先输入日期的方式。

在受支持的Java对象中有两个日期之后,您可以将它们相互比较。 This answer has an example how to do the comparison.