Skip to content

Instantly share code, notes, and snippets.

@jimmy087
Created May 4, 2013 12:40
Show Gist options
  • Save jimmy087/5517383 to your computer and use it in GitHub Desktop.
Save jimmy087/5517383 to your computer and use it in GitHub Desktop.
Displaying the first day of each month in java
import java.util.Scanner;
// Displaying the first day of each month.
public class Exercise4_28 {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the year: ");
int year = input.nextInt();
System.out.println("Please enter the day number: ");
int firstday = input.nextInt();
int numberOfDaysInMonth = 0;
for (int month = 1; month <= 12; month++) {
switch (month) {
case 1 : System.out.print("January 1 " + year + " is ");
numberOfDaysInMonth = 31;
break;
case 2 : System.out.print("February 1 " + year + " is ");
if (year % 4 == 0) numberOfDaysInMonth = 29;
else numberOfDaysInMonth = 28;
break;
case 3: System.out.print("March 1 " + year + " is ");
numberOfDaysInMonth = 31;
break;
case 4: System.out.print("April 1 " + year + " is ");
numberOfDaysInMonth = 30;
break;
case 5: System.out.print("May 1 " + year + " is ");
numberOfDaysInMonth = 31;
break;
case 6: System.out.print("June 1 " + year + " is ");
numberOfDaysInMonth = 30;
break;
case 7: System.out.print("July 1 " + year + " is ");
numberOfDaysInMonth = 31;
break;
case 8: System.out.print("August 1 " + year + " is ");
numberOfDaysInMonth = 31;
break;
case 9: System.out.print("September 1 " + year + " is ");
numberOfDaysInMonth = 30;
break;
case 10: System.out.print("October 1 " + year + " is ");
numberOfDaysInMonth = 31;
break;
case 11: System.out.print("November 1 " + year + " is ");
numberOfDaysInMonth = 30;
break;
case 12: System.out.print("December 1 " + year + " is ");
numberOfDaysInMonth = 31;
break;
}
switch (firstday) {
case 0 : System.out.println("Sunday.");
break;
case 1 : System.out.println("Monday.");
break;
case 2 : System.out.println("Tuesday.");
break;
case 3 : System.out.println("Wednesday.");
break;
case 4 : System.out.println("Thursday.");
break;
case 5 : System.out.println("Friday.");
break;
case 6 : System.out.println("Saturday.");
break;
}
firstday = (firstday + numberOfDaysInMonth) % 7;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment