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.
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