LocalDate Find all mondays

Write a program that reads a year and the number of a month (1-12) and prints dates of all Mondays of this month in the correct order (from the first to the last).

Sample Input 1:

2017 1

Sample Output 1:

2017-01-02
2017-01-09
2017-01-16
2017-01-23
2017-01-30
import java.time.LocalDate;

import java.util.Scanner;



class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);



        int year = scanner.nextInt();

        int month = scanner.nextInt();



        LocalDate date = LocalDate.of(year, month, 1);



        for (int i = 1; i <= date.lengthOfMonth(); i++) {

            if (LocalDate.of(year, month, i).getDayOfWeek().getValue() == 1) {

                System.out.println(LocalDate.of(year, month, i));

            }

        }

    }

}
posted @ 2020-08-13 12:45  longlong6296  阅读(128)  评论(0)    收藏  举报