1. /**
    2. * Display all of the Mondays in the current year and the specified month.
    3. */
    4. import java.time.Month;
    5. import java.time.Year;
    6. import java.time.DayOfWeek;
    7. import java.time.LocalDate;
    8. import java.time.DateTimeException;
    9. import java.time.temporal.TemporalAdjuster;
    10. import java.time.temporal.TemporalAdjusters;
    11. import java.io.PrintStream;
    12. import java.lang.NumberFormatException;
    13. public class ListMondays {
    14. public static void main(String[] args) {
    15. Month month = null;
    16. if (args.length < 1) {
    17. System.out.printf("Usage: ListMondays <month>%n");
    18. throw new IllegalArgumentException();
    19. }
    20. try {
    21. month = Month.valueOf(args[0].toUpperCase());
    22. } catch (IllegalArgumentException exc) {
    23. System.out.printf("%s is not a valid month.%n", args[0]);
    24. throw exc; // Rethrow the exception.
    25. }
    26. System.out.printf("For the month of %s:%n", month);
    27. LocalDate date = Year.now().atMonth(month).atDay(1).
    28. with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
    29. Month mi = date.getMonth();
    30. while (mi == month) {
    31. System.out.printf("%s%n", date);
    32. date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    33. mi = date.getMonth();
    34. }
    35. }
    36. }