1

I have to use the 4-4-5 accounting calendar in java.

I'm finding a way to have, given a specific date the current month, the corresponding accounting month.

For example, for the year 2020 I will have this accounting months:

29-Dec  25-Jan
26-Jan  22-Feb
23-Feb  28-Mar
29-Mar  25-Apr
26-Apr  23-May
24-May  27-Jun
28-Jun  25-Jul
26-Jul  22-Aug
23-Aug  26-Sep
27-Sep  24-Oct
25-Oct  21-Nov
22-Nov  26-Dec

So, for example, given a date 28 Apr I need to receive something like this:

26-Apr  23-May

I tried to use also ThreeTen-Extra project with AccountingChronology an AccountingChronologyBuilder. But, sincerely, I struggle to understand how to use it.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Safari
  • 11,437
  • 24
  • 91
  • 191
  • Can you show what you tried and also provide more details on the logic of the calendar? Why does it start on 29-Dec instead of 1-Jan? I assume it's because it's required to start on a certain day of the week, but you haven't specified which. – David Conrad May 08 '20 at 16:35

1 Answers1

2

Easy enough to get those dates using the ThreeTen-Extra library.

Define your chronology. Instantiate a AccountingChronology by using AccountingChronologyBuilder. Specify the attributes that fit your particular business practices. Notice how we specify 4-4-5 via the AccountingYearDivision.

AccountingChronology acctChrono = new AccountingChronologyBuilder()
        .endsOn(DayOfWeek.SATURDAY)
        .inLastWeekOf(Month.DECEMBER)
        .withDivision(AccountingYearDivision.QUARTERS_OF_PATTERN_4_4_5_WEEKS)
        .leapWeekInMonth(12)
        .toChronology();

Use that chronology to instantiate AccountingDate. Use a TemporalAdjuster to move from one date to another.

for (int month = 1; month <= 12; month++) {
    AccountingDate start = acctChrono.date(2020, month, 1);
    AccountingDate end = start.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println(start.format(DateTimeFormatter.ISO_LOCAL_DATE) + "  "
                     + end.format(DateTimeFormatter.ISO_LOCAL_DATE));
}

Output

2019-12-29  2020-01-25
2020-01-26  2020-02-22
2020-02-23  2020-03-28
2020-03-29  2020-04-25
2020-04-26  2020-05-23
2020-05-24  2020-06-27
2020-06-28  2020-07-25
2020-07-26  2020-08-22
2020-08-23  2020-09-26
2020-09-27  2020-10-24
2020-10-25  2020-11-21
2020-11-22  2020-12-26
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I was looking at the API, and two things I don't understand. Why leapWeekInMonth 12, and why inLastWeekOf instead of nearestEndOf? Are those just standard accounting conventions? I had gotten as far as creating the chronology (and converting some dates) but I had `.leapWeekInMonth(2)`. Admittedly, this is my first time looking at this API. – David Conrad May 08 '20 at 17:17
  • 2
    I just picked a value for `leapWeekInMonth`. It's up to you to pick the value you need/want. --- Try changing `inLastWeekOf()` to `nearestEndOf()` and see the difference for yourself. – Andreas May 08 '20 at 19:57
  • Thanks. I was just curious if there was a reason you picked the value you did. Appreciate the reply. – David Conrad May 08 '20 at 20:01
  • @BasilBourque I had deliberately not made it a link, since a link is already provided in the question, but thanks for the edit. – Andreas May 09 '20 at 16:20
  • @Andreas The Question linked to the Javadoc package page. I added link to the *ThreeTen-Extra* project page. I added more links to help guide the uninitiated. And thanks for your excellent Answer, very helpful. – Basil Bourque May 09 '20 at 16:32
  • @BasilBourque *FYI:* Using link references makes the answer source easier to read (and write). Use the link button when editing to automatically format/sort/merge link references. – Andreas May 09 '20 at 16:52