The problem
In this exercise, write a program that prompts the user to enter the year and first day of the year, and displays the first day of each month in the year on the console. For example, if the user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program should display the following output:
January 1, 2013 is Tuesday
...
December 1, 2013 is Sunday
Breaking it down
public static void main(String[] strings) {
Scanner input = new Scanner(System.in);
// Prompt user for year
System.out.print("Enter a year: ");
int year = input.nextInt();
input.close();
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("EEEE, MMM d, y ");
for (int month = 1; month <= 12; month++) {
LocalDate date = LocalDate.of(year, Month.of(month), 10);
LocalDate firstDayOfMonth = date.with(TemporalAdjusters
.firstDayOfMonth());
System.out.println(formatter.format(firstDayOfMonth));
}
}
Output
Enter a year: 2016
Friday, Jan 1, 2016
Monday, Feb 1, 2016
Tuesday, Mar 1, 2016
Friday, Apr 1, 2016
Sunday, May 1, 2016
Wednesday, Jun 1, 2016
Friday, Jul 1, 2016
Monday, Aug 1, 2016
Thursday, Sep 1, 2016
Saturday, Oct 1, 2016
Tuesday, Nov 1, 2016
Thursday, Dec 1, 2016