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 endOfMonth = ym.atEndOfMonth();
  34. if ( endOfMonth.getDayOfWeek().equals( dow ) ) {
  35. lastMondays.add( endOfMonth ); // Collect our last-monday-of-this-month.
  36. }
  37. // Set up the next loop.
  38. ym = ym.plusMonths( 1 );
  39. }
  40.  
  41. System.out.println( lastMondays );
  42.  
  43. }
  44. }
Success #stdin #stdout 0.14s 2184192KB
stdin
Standard input is empty
stdout
[2016-02-29, 2016-10-31, 2017-07-31]