-1

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;
}
  • https://stackoverflow.com/questions/4905416/how-do-i-add-one-month-to-current-date-in-java – Reginaldo Rigo Jun 14 '20 at 20:51
  • 1
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. – Basil Bourque Jun 14 '20 at 21:37
  • I too recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 15 '20 at 02:21
  • [How do I add or subtract date in Java 8?](https://kodejava.org/how-do-i-add-or-subtract-date-in-java-8/) – Ole V.V. Jun 15 '20 at 02:25

1 Answers1

3

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 ...
}
Lotsa
  • 412
  • 4
  • 11
  • 3
    It's better to replace `== -1` with `< 0`. Another implementation *may* return some other negative value, causing this code to fail. – MC Emperor Jun 14 '20 at 21:25
  • Thanks, I'll leave it in my answer, so people see the correction you suggest. – Lotsa Jun 14 '20 at 21:26
  • 2
    @Lotsa Comments are meant to improve the Answer/Question. I edited the Answer to incorporate the suggestion of `MC Emperor`. And I shortened your code by calling `LocalDate::minus`, linked to Javadoc, and added a heading as summary. – Basil Bourque Jun 14 '20 at 21:33
  • 2
    I further suggest replacing the use of `compareTo` with the `LocalDate` comparison methods `isEqual`, `isBefore`, & `isAfter`. Easier to read that way. – Basil Bourque Jun 14 '20 at 21:36
  • Thanks as well Basil. I thought of showing minusDays, but thought the OP might like to find out about the more universal minus functionality too, Days, Years, Months, etc. I like your edits though. All good in the collaboration shop! – Lotsa Jun 14 '20 at 21:37
  • What if these were two strings entered from an edittext widget? I am not looking for the current date but two selected dates. The user should not be able to select an end date that is only 25 days in the future. – Connor Daly Jun 14 '20 at 22:29
  • For example, if I entered 1/1/2020 the end date could not be 1/25/2020. It would have to be thirty days ahead of the entered date – Connor Daly Jun 14 '20 at 22:30
  • Dude, ... `LocalDate tomorrow = today.plusDays(1);` – Lotsa Jun 14 '20 at 22:42