/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.time.* ; import java.time.temporal.* ; import java.time.format.* ; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { { Year startYear = Year.of( 2016 ); Year stopYear = Year.of( 2018 ); YearMonth startYm = YearMonth.from( startYear.atDay( 1 ) ); YearMonth stopYm = YearMonth.from( stopYear.atDay( 1 ) ); int initialCapacity = ( int ) ChronoUnit.MONTHS.between( startYear.atDay( 1 ) , stopYear.atDay( 1 ) ); List< LocalDate > lastMondays = new ArrayList<>( initialCapacity ); DayOfWeek dow = DayOfWeek.MONDAY; TemporalAdjuster ta = TemporalAdjusters.lastInMonth( dow ); YearMonth ym = startYm; while ( ym.isBefore( stopYm ) ) { LocalDate firstOfMonth = ym.atDay( 1 ); LocalDate ld = firstOfMonth.with( ta ); lastMondays.add( ld ); // Collect our last-monday-of-this-month. // Set up the next loop. ym = ym.plusMonths( 1 ); } } }
Standard input is empty
[2016-01-25, 2016-02-29, 2016-03-28, 2016-04-25, 2016-05-30, 2016-06-27, 2016-07-25, 2016-08-29, 2016-09-26, 2016-10-31, 2016-11-28, 2016-12-26, 2017-01-30, 2017-02-27, 2017-03-27, 2017-04-24, 2017-05-29, 2017-06-26, 2017-07-31, 2017-08-28, 2017-09-25, 2017-10-30, 2017-11-27, 2017-12-25]