Right now, I have a method that checks that the start date is before the end date. How would I make sure that the start is a minimum of 30 days before the end date?
The code looks like this:
public static boolean CheckDates(String start_date, String end_date) {
SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
boolean b = false;
try {
if (dfDate.parse(start_date).before(dfDate.parse(end_date))) {
b = true; // If start date is before end date.
} else if (dfDate.parse(start_date).equals(dfDate.parse(end_date))) {
b = false; // If two dates are equal.
} else {
b = false; // If start date is after the end date.
}
} catch (ParseException e) {
e.printStackTrace();
}
return b;
}
LocalDate::minusDays
If you were using the newer java.time library, you get minus…
methods such as LocalDate::minusDays
.
LocalDate ld1 = LocalDate.now();
LocalDate ld2 = ld1.minusDays(31);
if ( ld2.isBefore( ld1.minusDays(30) ) ) {
// Yes, ld2 is earlier than 30 days before ld1 ...
}
Edit: The OP asked for different logic than my answer. Here's my response to the request in the comments.
LocalDate ld1 = LocalDate.parse("2020-06-14");
LocalDate ld2 = LocalDate.parse("2020-07-01");
if ( ld2.isBefore( ld1.plusDays(30) ) ) {
// Warning, ld2 is less than 30 days away from ld1 ...
}