Monthly Calender Program in Java

Introduction

  • In this blog, I am going to explain about the monthly calendar program in Java

Software Requirements

  • Java, Notepad.

Program

  1. import java.util.*;  
  2. import java.text.*;  
  3.   
  4. public class MonthCalender {  
  5.       
  6.     public final static String[] monthcalender = {  
  7.         "January""February""March""April""May""June""July",  
  8.             "August""September""October""November""December"};  
  9.           
  10.         public final static int daysinmonths[] = {312831303130313130313031 };  
  11.           
  12.         private void displayMonth(int month, int year) {  
  13.               
  14.             // The number of days to leave blank at  
  15.             // the start of this month.  
  16.               
  17.             int blankdays = 0;  
  18.             System.out.println("  " + monthcalender[month] + " " + year);  
  19.               
  20.             if (month < 0 || month > 11) {  
  21.                 throw new IllegalArgumentException(  
  22.                     "Month " + month + " is not valid and must lie in between 0 and 11");  
  23.             }  
  24.               
  25.             GregorianCalendar cldr = new GregorianCalendar(year, month, 1);  
  26.             System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday");  
  27.               
  28.             // Compute how much to leave before before the first day of the month.  
  29.             // getDay() returns 0 for Sunday.  
  30.               
  31.             blankdays = cldr.get(Calendar.DAY_OF_WEEK)-1;  
  32.             int daysInMonth = daysinmonths[month];  
  33.               
  34.             if (cldr.isLeapYear(cldr.get(Calendar.YEAR)) && month == 1) {  
  35.                   
  36.                 ++daysInMonth;  
  37.             }  
  38.               
  39.             // Blank out the labels before 1st day of the month  
  40.             for (int i = 0; i < blankdays; i++) {  
  41.                 System.out.print("   ");  
  42.             }  
  43.               
  44.             for (int i = 1; i <= daysInMonth; i++) {  
  45.                   
  46.                 // This "if" statement is simpler than messing with NumberFormat  
  47.                 if (i<=9) {  
  48.                     System.out.print(" ");  
  49.                 }  
  50.                 System.out.print(i);  
  51.   
  52.                 if ((blankdays + i) % 7 == 0) { // Wrap if EOL  
  53.                 System.out.println();  
  54.                 }  
  55.                 else {  
  56.                     System.out.print(" ");  
  57.                 }  
  58.             }  
  59.         }  
  60.           
  61.     /** 
  62.      * Sole entry point to the class and application. 
  63.      * @param args Array of String arguments. 
  64.      */  
  65.     public static void main(String[] args) {  
  66.           
  67.         int mon, yr;  
  68.         MonthCalender moncldr = new MonthCalender();  
  69.   
  70.         if (args.length == 2) {  
  71.             moncldr.displayMonth(Integer.parseInt(args[0])-1, Integer.parseInt(args[1]));  
  72.         }  
  73.         else {  
  74.             Calendar todaycldr = Calendar.getInstance();  
  75.             moncldr.displayMonth(todaycldr.get(Calendar.MONTH), todaycldr.get(Calendar.YEAR));  
  76.         }  
  77.     }  

Output

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now