0

I'm working on an accounting reporting and one of the report requires a list of all post dates in last four weeks from the given date.

A post date is a last day of the week, in our case is it Sunday OR a last day of the month.

For example. If I enter 12/1/2019 then I'll the following list:

  • Week 1: 12/01/2019 - 12/01/2019
  • Week 1: 11/25/2019 - 11/30/2019
  • Week 2: 11/18/2019 - 11/24/2019
  • Week 3: 11/11/2019 - 11/17/2019
  • Week 4: 11/04/2019 - 11/10/2019

As you see here, there are 5 post dates. Some times there are 4 weeks.

Here is an example of 4 post dates: End week 10/27/2019

  • Week 1: 10/21/2019 - 10/27/2019
  • Week 2: 10/14/2019 - 10/20/2019
  • Week 3: 10/07/2019 - 10/13/2019
  • Week 4: 10/01/2019 - 10/06/2019

Input value will always be Sunday, which is the end of the week.

I was wondering if there is a tested library that has this kind of functionality already instead of writing my own logic to deal with the dates.

Maksim
  • 16,635
  • 27
  • 94
  • 135
  • 3
    This question's answer provides code to do just that: https://stackoverflow.com/a/30966945/217269 – spork Dec 02 '19 at 19:47
  • Not exactly that, @spork, but a great deal of the job. Thank you for a definitely helpful link. – Ole V.V. Dec 03 '19 at 02:41

1 Answers1

2

java.time

I doubt that a tested library provides exactly what you are asking for, though you may try your luck with your search engine. With java.time, the modern Java date and time API, it’s not hard to code it yourself. There are a number of ways to go about it. The following would feel natural to me:

public static List<LocalDate> getPostDates(LocalDate endDate) {
    DayOfWeek dow = endDate.getDayOfWeek();
    if (! dow.equals(DayOfWeek.SUNDAY)) {
        throw new IllegalArgumentException("End date must be Sunday, was " + dow);
    }

    List<LocalDate> result = new ArrayList<>(5);
    for (int week = 1; week <= 4; week++) {
        LocalDate lastDayOfWeek = endDate.minusWeeks(week - 1);
        result.add(lastDayOfWeek);
        // If the week crosses a month boundary, also add last day of month
        int dom = lastDayOfWeek.getDayOfMonth();
        if (dom < 7) {
            result.add(lastDayOfWeek.minusDays(dom));
        }
    }

    return result;
}

Let’s try it out with your two sample dates:

    System.out.println(getPostDates(LocalDate.of(2019, Month.DECEMBER, 1)));
    System.out.println(getPostDates(LocalDate.of(2019, Month.OCTOBER, 27)));

Output is:

[2019-12-01, 2019-11-30, 2019-11-24, 2019-11-17, 2019-11-10]
[2019-10-27, 2019-10-20, 2019-10-13, 2019-10-06, 2019-09-30]

I noticed that the last example included September 30, which wasn’t mentioned in your question. It’s within the last four weeks from October 27, though, and is the end of a month, so I would think it was correct?

Examples with just 4 post dates:

    System.out.println(getPostDates(LocalDate.of(2019, Month.SEPTEMBER, 29)));
    System.out.println(getPostDates(LocalDate.of(2019, Month.JULY, 14)));
[2019-09-29, 2019-09-22, 2019-09-15, 2019-09-08]
[2019-07-14, 2019-07-07, 2019-06-30, 2019-06-23]

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Good one! Now I start loving this new (to Java 8) java.time API. Thanks for pointing that out! – Maksim Dec 03 '19 at 16:10