daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. package calendar;
  3.  
  4. import java.util.Scanner;
  5. class Calendar {
  6.   public static void main(String[] args) {
  7.     Scanner input = new Scanner(System.in);
  8.  
  9.     // prompt the user to enter the year
  10.     System.out.print("Enter full year (eg., 2001)");
  11.     int year = input.nextInt();
  12.  
  13.      // promt the user to enter the month
  14.      System.out.print("Enter the month as a number (eg., 6 for June) ");
  15.      int month = input.nextInt ();
  16.  
  17.     // Print Calendar for the month of the year
  18.     printMonth(year, month);
  19.   }
  20.  
  21.   // print the calendar for a month in a year
  22.   public static void printMonth(int year, int month){
  23.       // print the headings of the calendar
  24.       printMonthTitle(year,month);
  25.       // print the body of the calenda
  26.       printMonthBody (year,month);
  27.   }
  28.  
  29.   // printh the month title, eg., March 2012
  30.   public static void printMonthTitle(int year, int month){
  31.       System.out.println("        " + getMonthName(month)+ " " +year);
  32.       System.out.println("-----------------------------");
  33.       System.out.println(" Sun Mon Tue Wed Thu Fri Sat ");
  34.   }
  35.   // get the name for the month
  36.   public static String getMonthName(int month) {
  37.       String monthName = "";
  38.       switch (month){
  39.       case 1:monthName = "January"; break;
  40.       case 2:monthName = "February"; break;
  41.       case 3:monthName = "March"; break;
  42.       case 4:monthName = "April"; break;
  43.       case 5:monthName = "May"; break;
  44.       case 6:monthName = "June"; break;
  45.       case 7:monthName= "July"; break;
  46.       case 8:monthName = "August"; break;
  47.       case 9:monthName = "September"; break;
  48.       case 10:monthName = "October"; break;
  49.       case 11:monthName = "November"; break;
  50.       case 12:monthName = "December";
  51.   }
  52.   return monthName;
  53.   }
  54.  
  55.   // Print month body
  56.   public static void printMonthBody(int year, int month){
  57.       // Get start day of the week for the first date in the month
  58.       int startDay = getStartDay(year, month);
  59.       // Get number of days in the month
  60.       int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
  61.       // pad space before the first day of the month
  62.       int i=0;
  63.       for (i=0; i< startDay; i++)
  64.           System.out.print("   ");
  65.       for (i=1; i<=numberOfDaysInMonth; i++){
  66.           System.out.printf("%4d",i);
  67.           if ((i + startDay)% 7 == 0)
  68.               System.out.println();
  69.       }
  70.       System.out.println();
  71.   }
  72.   // Get the start day of month/1/year
  73.   public static int getStartDay(int year, int month){
  74.       final int START_DAY_FOR_JAN_1_1800 = 3;
  75.       // get total number of days from 1/1/1800 to month/1/year
  76.       int totalNumberOfDays = getTotalNumberOfDays(year,month);
  77.       // return the start day for month/1/year
  78.       return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
  79.   }
  80.   // Get the total number of days since January 1, 1800
  81.   public static int getTotalNumberOfDays(int year, int month){
  82.       int total = 0;
  83.       // get the total days from 1800 to 1/1/year
  84.       for (int i = 1800; i < year; i++)
  85.           if (isleapYear(i))
  86.               total = total + 366;
  87.           else
  88.               total = total + 365;
  89.       // add days from Jan to the month prior to the calendar month
  90.       for (int i =1; i < month; i++)
  91.         total = total + getNumberOfDaysInMonth(year,i);
  92.     return total;
  93.   }
  94.   // Get the number of days in a month
  95.   public static int getNumberOfDaysInMonth(int year, int month){
  96.       if (month ==1 || month ==3 || month ==5 || month == 7 || month ==8 ||
  97.               month == 10 || month == 12)
  98.           return 31;
  99.       if (month == 4 || month == 6 || month == 9 || month == 11)
  100.           return 30;
  101.       if (month == 2) return isleapYear(year) ? 29:28;
  102.       return 0; // if month is incorrect
  103.   }
  104.   // Determine if it is a leap year
  105.   public static boolean isleapYear(int year){
  106.       return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
  107.   }
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top