fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.* ;
  8. import java.time.temporal.* ;
  9. import java.time.format.* ;
  10.  
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15. public static void main (String[] args) throws java.lang.Exception
  16. {
  17.  
  18. Year startYear = Year.of( 2016 );
  19. Year stopYear = Year.of( 2018 );
  20.  
  21. YearMonth startYm = YearMonth.from( startYear.atDay( 1 ) );
  22. YearMonth stopYm = YearMonth.from( stopYear.atDay( 1 ) );
  23.  
  24. int initialCapacity = ( int ) ChronoUnit.MONTHS.between( startYear.atDay( 1 ) , stopYear.atDay( 1 ) );
  25. List< LocalDate > lastMondays = new ArrayList<>( initialCapacity );
  26.  
  27. DayOfWeek dow = DayOfWeek.MONDAY;
  28.  
  29. TemporalAdjuster ta = TemporalAdjusters.lastInMonth( dow );
  30.  
  31. YearMonth ym = startYm;
  32. while ( ym.isBefore( stopYm ) ) {
  33. LocalDate firstOfMonth = ym.atDay( 1 );
  34. LocalDate ld = firstOfMonth.with( ta );
  35. lastMondays.add( ld ); // Collect our last-monday-of-this-month.
  36. // Set up the next loop.
  37. ym = ym.plusMonths( 1 );
  38. }
  39.  
  40. System.out.println( lastMondays );
  41.  
  42. }
  43. }
Success #stdin #stdout 0.22s 2184192KB
stdin
Standard input is empty
stdout
[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]