I have two datepickers for search parameters but i cant have the search more then a week(to much of a load on the server) so i want to check if the first date is before the second one, and if the first date and second date are less then 7 days apart. if all is correct then return a true boolean if not false.
public Calendar max = Calendar.getInstance();
public boolean checkweek(Calendar cStart, Calendar cEnd) {
Toast.makeText(this,
cStart.DAY_OF_MONTH + " - " + cStart.MONTH + " - " + cStart.YEAR, Toast.LENGTH_LONG).show();
if (cEnd.after(cStart) || ((cEnd.DAY_OF_MONTH == cStart.DAY_OF_MONTH) && (cEnd.MONTH == cStart.MONTH))) {
max.set(Calendar.YEAR, cStart.YEAR);
max.set(Calendar.MONTH, cStart.MONTH);
max.set(Calendar.DAY_OF_MONTH, cStart.DAY_OF_MONTH);
max.add(Calendar.DAY_OF_MONTH, 7);
Toast.makeText(this,
max.DAY_OF_MONTH + " - " + max.MONTH + " - " + max.YEAR,
Toast.LENGTH_LONG).show();
if (cEnd.before(max)) {
return true;
}
else{
return false;
}
}
else{
return false;}
}
But it throws a null exception. im declaring the cStart and cEnd so i have no idea what it is…
Why not just use math for this?
Or, thanks to @Sam, you can make this simpler using
DateUtils.WEEK_IN_MILLIS:How it works:
Calendar.getTime().getTime()will return alongof the milliseconds value. Just subtract them (to get the difference), then check if it’s greater than604800000L(milliseconds in a week, per above formula).This should provide no
NullPointerExceptionif run whencStartandcEndare non-null.