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));
}
}
}
}

浙公网安备 33010602011771号