Skip to content

Instantly share code, notes, and snippets.

@feynmanliang
Created March 8, 2012 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feynmanliang/2003854 to your computer and use it in GitHub Desktop.
Save feynmanliang/2003854 to your computer and use it in GitHub Desktop.
Quick calendar hack-up for friend
import java.util.Scanner;
public class calendar {
// initialize static scanned
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int month = getMonth();
int year = getYear();
System.out.println();
printCalendar(month, year);
}
// get and validate month input
public static int getMonth() {
int month = 0;
while ((month < 1) || (month > 12)) {
System.out.println("Enter month number (1-12):");
month = input.nextInt();
if ((month < 1) || (month > 12)) {
System.out.println("Month must be an int between 1-12!");
}
}
return month;
}
// return string name for a month number
public static String nameMonth(int month) {
String[] months = {"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
return months[month-1];
}
// get and validate year input
public static int getYear() {
int year = 0;
while ((year < 1901) || (year > 2100)) {
System.out.println("Enter year (1901-2100): ");
year = input.nextInt();
if ((year < 1901) || (year > 2100)) {
System.out.println("Year must be an int between 1901-2100!");
}
}
return year;
}
// calculate new years day
public static int newYearsDay (int year) {
return (2 + (year-1901) + (year-1901)/4) % 7;
}
// tests for leap year
public static Boolean isLeapYear (int year) {
if ((year % 4) == 0) {
return true;
}
return false;
}
// returns number of days in month
public static int daysInMonth (int month, int year) {
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// february has 29 days in leap year
if (isLeapYear(year)) days[1] = 29;
return days[month-1];
}
// calculates first day in month of year
public static int firstDayMonth (int month, int year) {
int newYears = newYearsDay (year);
// sum up the number of days prior to current month
int daysIntoYear = 0;
for (int i = 1; i < month; i++) {
daysIntoYear += daysInMonth(i, year);
}
// Letting 0 = Sunday, return the integer offset from 0
return (daysIntoYear % 7);
}
public static void printCalendar(int month, int year) {
int firstDay = firstDayMonth(month, year);
int numDays = daysInMonth(month, year);
// print title
String title = nameMonth(month) + " " + year;
System.out.printf ("%16s \n", title);
// print letter week days
String[] letterDays = {"S", "M", "Tu", "W", "Th", "F", "S"};
for (int day = 0; day < letterDays.length; day++) {
System.out.printf ("%2s ", letterDays[day]);
}
System.out.println();
// prints blanks for first day's offset from sunday
// spotsFilled keeps track of entries so breaks can be inserted
int spotsFilled = 0;
for (int i=0; i < firstDay; i++) {
System.out.printf ("%2s ", "");
++spotsFilled;
}
// prints the remaining days in the month
for (int j=1; j <= numDays; j++) {
// breaks inserted every 7 days
if ((spotsFilled % 7) == 0)
System.out.println();
System.out.printf ("%2d ", j);
++spotsFilled;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment