Implementing Monthly Calendar Program In JAVA

Introduction

In this blog, we will learn how to get started with Monthly Calendar Program in Java.
 
Software Requirement

JAVA, NOTEPAD. 
  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[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };    
  11.             
  12.         private void displayMonth(int month, int year) {    
  13.                    
  14.             int blankdays = 0;    
  15.             System.out.println("  " + monthcalender[month] + " " + year);    
  16.                 
  17.             if (month < 0 || month > 11) {    
  18.                 throw new IllegalArgumentException(    
  19.                     "Month " + month + " It is not valid and must lie in between 0 and 11");    
  20.             }    
  21.                 
  22.             GregorianCalendar cldr = new GregorianCalendar(year, month, 1);    
  23.             System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday");    
  24.                               
  25.                 
  26.             blankdays = cldr.get(Calendar.DAY_OF_WEEK)-1;    
  27.             int daysInMonth = daysinmonths[month];    
  28.                 
  29.             if (cldr.isLeapYear(cldr.get(Calendar.YEAR)) && month == 1) {    
  30.                     
  31.                 ++daysInMonth;    
  32.             }    
  33.                              
  34.             for (int i = 0; i < blankdays; i++) {    
  35.                 System.out.print("   ");    
  36.             }    
  37.                 
  38.             for (int i = 1; i <= daysInMonth; i++) {    
  39.                                    
  40.                 if (i<=9) {    
  41.                     System.out.print(" ");    
  42.                 }    
  43.                 System.out.print(i);    
  44.     
  45.                 if ((blankdays + i) % 7 == 0) { 
  46.                 System.out.println();    
  47.                 }    
  48.                 else {    
  49.                     System.out.print(" ");    
  50.                 }    
  51.             }    
  52.         }    
  53.                 
  54.     public static void main(String[] args) {    
  55.             
  56.         int mon, yr;    
  57.         MonthCalender moncldr = new MonthCalender();    
  58.     
  59.         if (args.length == 2) {    
  60.             moncldr.displayMonth(Integer.parseInt(args[0])-1, Integer.parseInt(args[1]));    
  61.         }    
  62.         else {    
  63.             Calendar todaycldr = Calendar.getInstance();    
  64.             moncldr.displayMonth(todaycldr.get(Calendar.MONTH), todaycldr.get(Calendar.YEAR));    
  65.         }    
  66.     }    
  67. }    
Output

 
 
Monthly Calendar has been created successfully. Feel free to comment with some suggesstions.  
Ebook Download
View all
Learn
View all