fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.* ;
  7. import java.util.stream.* ;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. LocalDate start = LocalDate.parse( "2023-01-03" );
  15. DayOfWeek dayOfWeek = start.getDayOfWeek();
  16. LocalDate stop = start.withDayOfMonth( 1 ).plusMonths( 1 ); // Using Half-Open approach, running up to but not including first day of following month. End date is *exclusive* while the start date is *inclusive*.
  17.  
  18. List < LocalDate > datesInSameMonthWithSameDayOfWeek =
  19. start
  20. .datesUntil( stop )
  21. .filter(
  22. date -> date.getDayOfWeek().equals( dayOfWeek )
  23. )
  24. .collect( Collectors.toUnmodifiableList() ) // In Java 16+, simplify to .toList().
  25. ;
  26.  
  27. System.out.println( datesInSameMonthWithSameDayOfWeek );
  28. }
  29. }
Success #stdin #stdout 0.13s 49716KB
stdin
Standard input is empty
stdout
[2023-01-03, 2023-01-10, 2023-01-17, 2023-01-24, 2023-01-31]