使用ThreeTen-Extra库很容易获得这些日期。
定义你的年表。AccountingChronology
使用实例化 a AccountingChronologyBuilder
。指定适合您的特定业务实践的属性。请注意我们如何通过AccountingYearDivision
.
AccountingChronology acctChrono = new AccountingChronologyBuilder()
.endsOn(DayOfWeek.SATURDAY)
.inLastWeekOf(Month.DECEMBER)
.withDivision(AccountingYearDivision.QUARTERS_OF_PATTERN_4_4_5_WEEKS)
.leapWeekInMonth(12)
.toChronology();
使用该年表来实例化AccountingDate
. 使用 aTemporalAdjuster
从一个日期移动到另一个日期。
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));
}
输出
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