-3

Can someone suggest the logic to find out the no. of Mondays between two dates in Java?

Instead of looping through all the days, is there any other approach to count the no. of occurrences of Mondays between two dates in java

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
shruthi
  • 21
  • 1
  • 2
  • 3
    could you post your code so that we could understand what approach you are taking – Blip Jun 15 '17 at 13:23
  • 2
    What do you mean by "*between* two dates"? Are starting and ending dates included or excluded in searching range? Lets say that starting date is some Monday and ending day is next Monday. Should result be 0 Mondays between or maybe 2? – Pshemo Jun 15 '17 at 13:55
  • Always search Stack Overflow thoroughly before posting. You can assume any basic date-time topic has already been asked and answered. – Basil Bourque Jun 15 '17 at 16:37

2 Answers2

7

There’s more than one way to go. Here’s a suggestion:

public static long noOfMondaysBetween(LocalDate first, LocalDate last) {
    if (last.isBefore(first)) {
        throw new IllegalArgumentException("first " + first + " was after last " + last);
    }
    // find first Monday in interval
    LocalDate firstMonday = first.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    // similarly find last Monday
    LocalDate lastMonday = last.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
    // count
    long number = ChronoUnit.WEEKS.between(firstMonday, lastMonday);
    // add one to count both first Monday and last Monday in
    return number + 1;
}

For example, noOfMondaysBetween(LocalDate.of(2017, Month.JUNE, 15), LocalDate.of(2017, Month.JUNE, 15)) returns 0. It may be a little subtle that the code takes this case into account: First Monday is June 19 and last is June 12. Count of weeks between the two Mondays is -1, so when I add 1, the result is 0, which is correct. To count the Mondays in June:

    System.out.println(noOfMondaysBetween(LocalDate.of(2017, Month.MAY, 31), LocalDate.of(2017, Month.JULY, 1)));

Result:

4

If you intended to include the first date in the count (if it is a Monday), use nextOrSame(DayOfWeek.MONDAY) instead of next(DayOfWeek.MONDAY). Similarly to include the second date use previousOrSame(DayOfWeek.MONDAY).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

I'm not a Java coder but I'm a coder. Here's how I'd solve this:

  1. Count the days between the two dates (aka DATESPAN). I'm sure Java has a function for that.

  2. Get the 'Day of Week' (AS A NUMBER, assuming that Monday = 1 )of both dates. I'm sure Java has a function for this too. We need to know if either is a Monday.

  3. If DATESPAN < 7 Use this logic:

    Answer = End Date Number > DATESPAN ? 0 : 1
    
  4. IF DATESPAN >=7 CONTINUE TO GET ANSWER:

  5. Divide the DATESPAN by 7.

  6. If there is a remainder from the division, use the floor value of the quotient for the answer.

  7. If there is NO remainder, check the start date and end date. If either are a Monday the quotient is the answer, If not the quotient - 1 is the answer

wittich
  • 2,079
  • 2
  • 27
  • 50
Scotty
  • 130
  • 13
  • 1
    It’s not bulletproof. June 12 was a Monday. If I take June 10 and June 14, the answer should be 1. The division yields 0 with a remainder of 4, so according to your item 4), 0 should be the answer. But you’re close. – Ole V.V. Jun 15 '17 at 15:11
  • oops, didn't consider if the time difference was less than 7 days. – Scotty Jun 15 '17 at 16:39
  • I updated my answer above to consider a date span of less than seven days – Scotty Jun 15 '17 at 17:03