The problem Write a program that prompts the user to enter the year and first day of the year and displays the calendar table for 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 calendar for each month in the year.
Breaking it down public static void main ( String [] strings ) {
Scanner input = new Scanner ( System . in );
System . out . print ( "Enter a year: " );
int year = input . nextInt ();
System . out . print ( "Enter the first day of the year: " );
int startDay = input . nextInt ();
input . close ();
int numberOfDaysInMonth = 0 ;
for ( int month = 1 ; month <= 12 ; month ++) {
LocalDate date = LocalDate . of ( year , Month . of ( month ), 10 );
String monthDisplayName = date . getMonth (). getDisplayName (
TextStyle . FULL , Locale . US );
numberOfDaysInMonth = date . getMonth (). length ( Year . isLeap ( year ));
System . out . print ( " " );
System . out . println ( monthDisplayName + " " + year );
System . out . println ( "-----------------------------" );
System . out . println ( " Sun Mon Tue Wed Thu Fri Sat" );
for ( int i = 0 ; i < startDay ; i ++) {
System . out . print ( " " );
}
for ( int i = 1 ; i <= numberOfDaysInMonth ; i ++) {
if ( i < 10 ) {
System . out . print ( " " + i );
} else {
System . out . print ( " " + i );
}
if (( i + startDay ) % 7 == 0 ) {
System . out . println ();
}
}
System . out . println ( "" );
startDay = ( startDay + numberOfDaysInMonth ) % 7 ;
}
}
Output Enter a year: 2016
Enter the first day of the year: 3
January 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
February 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
March 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
April 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
May 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
June 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
July 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
August 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
September 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
October 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
November 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
December 2016
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Display calendars posted by Justin Musgrove on 22 April 2016
Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch5
Share on: Facebook Google+