Reputation: 31
I'm new to Android programming and I'm currently developing an app. Can someone help me on how to validate the date that is being input by the user if is it in range 7 days? so, i have two string 'startDay' and 'endDay' where user input startDay = 1/2/2017 endDay = 6/2/2017
then it went on the next step. and if user input startDay = 1/2/2017 endDay = 9/2/2017 then it return message 'maximum 7 days'
there is the code that i made
@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();
}
}
}
but when im input startDay = 22/2/2017 endDay = 25/2/2017
the result is 'maximum 7 days' it should be return to the next step
please help me..im searched and tried also but not get such a solution..
Upvotes: 0
Views: 626
Reputation: 1764
I added startYearStr
, please note that.
Use below code :
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
gives you the day difference between two dates.
Upvotes: 1
Reputation: 2030
To compare dates you would want to use the Java Date
or Calendar
objects but reading in the initial dates from an EditText like you are doing wouldn't be safe. If you aren't validating the input, a user could enter in something like "February 2nd 2016" when you are expecting "2/2/2016" so you might want to change it from a freeform EditText to a date picker so you can control how the user inputs the dates first.
Once you have the two dates in a supported Java object you can compare them against each other. This answer has an example how to do the comparison.
Upvotes: 0