Create Calendar program in Java

Hello everyone, Welcome to my new post in this post i'm going to share my code "Calendar" program written in Java Language. In this post I will tell you how you can create a calendar in java. If you have any views and suggestion about this post so feel free to comment.

About Program

calendar

It is a simple program in you have to enter the year and month number and this program will give you the calendar of given year and month on console window. Read the complete post to understand the code.

Let's Start

I have used many comments in program so you can easily understand the program.I will tell main parts of program and hope you can understand the code and make your own Calendar. If you need any help or something that you did not understand comment below i will answer your Query.

So In this program I'm importing scanner class for taking input from user

  1. //import modules
  2. import java.util.Scanner;



I have created two classes main and calendar class  

main class
I made this class just for creating the object of calendar class and for calling methods of calendar class


  1. class main
  2. {
  3. public static void main(String args[])
  4. {
  5. calender obj=new calender();
  6. obj.display();
  7. }
  8. }

calendar class
This class is the import part of class it contains methods for calculation and printing of calendar.It's methods are display() and print_cal().

  1. class calender
  2. {
  3. public String month_name=new String();
  4. /*used to Calculation and display calendar*/
  5. public void display()
  6. {
  7. Scanner sc=new Scanner(System.in);
  8. int left,user_year,start_year,day=1,r=0,leap_y=0,i=0,month,last_day,first_day;
  9. start_year=1973;
  10. System.out.println("  *Instruction :- This program will give you calender of any year after 1972");
  11. //enter year to check calender
  12. System.out.println("\nEnter year: ");
  13. user_year=sc.nextInt();
  14. if(start_year>user_year)
  15. {
  16. left=start_year-user_year;
  17. }
  18. else
  19. {
  20. left=user_year-start_year;
  21. }
  22. //counting number of leap year
  23. for(i=start_year;i<user_year;i++)
  24. {
  25. if(i%4==0)
  26. { if(user_year==1000 || user_year==2000 || user_year==3000 || user_year==4000 || user_year==5000 )
  27. {
  28. if(user_year%4==0 && user_year%400==0)
  29. leap_y=leap_y+1;
  30. }
  31. else
  32. leap_y=leap_y+1;
  33. }
  34. }
  35. left=left+leap_y;
  36. r=left%7;
  37. r=r+1;
  38. System.out.println("Enter month(1-12): ");
  39. month=sc.nextInt();
  40. if(user_year%4==0)
  41. { if(user_year==1000 || user_year==2000 || user_year==3000 || user_year==4000 || user_year==5000 )
  42. { if(user_year%400==0)
  43. {
  44. first_day=29;
  45. last_day=29;
  46. }
  47. else
  48. {
  49. first_day=28;
  50. last_day=28;
  51. }
  52. }
  53. else
  54. {
  55. first_day=29;
  56. last_day=29;
  57. }
  58. }
  59. else
  60. {
  61. first_day=28;
  62. last_day=28;
  63. }
  64. /*used to get the month name*/
  65. switch(month)
  66. { case 1:
  67. month_name="JANUARY";
  68. last_day=31;
  69. break;
  70. case 2:
  71. r=r+31;r=r%7;month_name="FEBRUARY";
  72. break;
  73. case 3:
  74. r=r+first_day+31;r=r%7;
  75. month_name="MARCH";
  76. last_day=31;
  77. break;
  78. case 4:
  79. r=r+(31*2)+first_day;r=r%7;
  80. month_name="APRIL";
  81. last_day=30;
  82. break;
  83. case 5:r=r+30+first_day+(31*2);r=r%7;month_name="MAY";last_day=31;break;
  84. case 6:r=r+30+first_day+(31*3);r=r%7;month_name="JUNE";last_day=30;break;
  85. case 7:r=r+(30*2)+first_day+(31*3);r=r%7;month_name="JULY";last_day=31;break;
  86. case 8:r=r+(30*2)+first_day+(31*4);r=r%7;month_name="AUGUST";last_day=31;break;
  87. case 9:r=r+(30*2)+first_day+(31*5);r=r%7;month_name="SEPTEMBER";last_day=30;break;
  88. case 10:r=r+(30*3)+first_day+(31*5);r=r%7;month_name="OCTOBER";last_day=31;break;
  89. case 11:r=r+(30*3)+first_day+(31*6);r=r%7;month_name="NOVEMBER";last_day=30;break;
  90. case 12:r=r+(30*4)+first_day+(31*6);r=r%7;month_name="DECEMBER";last_day=31;break;
  91. default:System.out.println("\n\t*******Wrong Input*******");System.exit(0);break;
  92. }
  93. /*printing calender*/
  94. System.out.println("\t\t\t\t"+month_name+" "+user_year+"\n");
  95. if(user_year>3000)
  96. {
  97. r=r-1;
  98. }
  99. print_cal(r,last_day);
  100. }
  101. /*printing dates*/
  102. public void print_cal(int r,int last_day)
  103. {
  104. int i=0,day=1;
  105. System.out.println("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n\n");
  106. while(i<r)
  107. {
  108. System.out.print("\t*");
  109. i++;
  110. }
  111. for(i=r;i<7;i++)
  112. {
  113. System.out.print("\t"+day);
  114. day++;
  115. }
  116. while(day<=last_day)
  117. {
  118. System.out.println();
  119. for(i=1;i<=7;i++)
  120. { if(day==last_day+1)
  121. {
  122. break;
  123. }
  124. System.out.print("\t"+day);
  125. day++;
  126. }
  127. }
  128. System.out.println("\n\n\t***********************************************************");
  129. }
  130. }

Now you can download the whole code in combined way from here.
I'm also pasting the whole code so can copy from here too.

  1. import java.util.Scanner;
  2. class calender
  3. {
  4. public String month_name=new String();
  5. /*used to Calcution and display calender*/
  6. public void display()
  7. {
  8. Scanner sc=new Scanner(System.in);
  9. int left,user_year,start_year,day=1,r=0,leap_y=0,i=0,month,last_day,first_day;
  10. start_year=1973;
  11. System.out.println("  *Instruction :- This program will give you calender of any year after 1972");
  12. //enter year to check calender
  13. System.out.println("\nEnter year: ");
  14. user_year=sc.nextInt();
  15. if(start_year>user_year)
  16. {
  17. left=start_year-user_year;
  18. }
  19. else
  20. {
  21. left=user_year-start_year;
  22. }
  23. //counting number of leap year
  24. for(i=start_year;i<user_year;i++)
  25. {
  26. if(i%4==0)
  27. { if(user_year==1000 || user_year==2000 || user_year==3000 || user_year==4000 || user_year==5000 )
  28. {
  29. if(user_year%4==0 && user_year%400==0)
  30. leap_y=leap_y+1;
  31. }
  32. else
  33. leap_y=leap_y+1;
  34. }
  35. }
  36. left=left+leap_y;
  37. r=left%7;
  38. r=r+1;
  39. System.out.println("Enter month(1-12): ");
  40. month=sc.nextInt();
  41. if(user_year%4==0)
  42. { if(user_year==1000 || user_year==2000 || user_year==3000 || user_year==4000 || user_year==5000 )
  43. { if(user_year%400==0)
  44. {
  45. first_day=29;
  46. last_day=29;
  47. }
  48. else
  49. {
  50. first_day=28;
  51. last_day=28;
  52. }
  53. }
  54. else
  55. {
  56. first_day=29;
  57. last_day=29;
  58. }
  59. }
  60. else
  61. {
  62. first_day=28;
  63. last_day=28;
  64. }
  65. /*used to get the month name*/
  66. switch(month)
  67. { case 1:
  68. month_name="JANUARY";
  69. last_day=31;
  70. break;
  71. case 2:
  72. r=r+31;r=r%7;month_name="FEBRUARY";
  73. break;
  74. case 3:
  75. r=r+first_day+31;r=r%7;
  76. month_name="MARCH";
  77. last_day=31;
  78. break;
  79. case 4:
  80. r=r+(31*2)+first_day;r=r%7;
  81. month_name="APRIL";
  82. last_day=30;
  83. break;
  84. case 5:r=r+30+first_day+(31*2);r=r%7;month_name="MAY";last_day=31;break;
  85. case 6:r=r+30+first_day+(31*3);r=r%7;month_name="JUNE";last_day=30;break;
  86. case 7:r=r+(30*2)+first_day+(31*3);r=r%7;month_name="JULY";last_day=31;break;
  87. case 8:r=r+(30*2)+first_day+(31*4);r=r%7;month_name="AUGUST";last_day=31;break;
  88. case 9:r=r+(30*2)+first_day+(31*5);r=r%7;month_name="SEPTEMBER";last_day=30;break;
  89. case 10:r=r+(30*3)+first_day+(31*5);r=r%7;month_name="OCTOBER";last_day=31;break;
  90. case 11:r=r+(30*3)+first_day+(31*6);r=r%7;month_name="NOVEMBER";last_day=30;break;
  91. case 12:r=r+(30*4)+first_day+(31*6);r=r%7;month_name="DECEMBER";last_day=31;break;
  92. default:System.out.println("\n\t*******Wrong Input*******");System.exit(0);break;
  93. }
  94. /*printing calender*/
  95. System.out.println("\t\t\t\t"+month_name+" "+user_year+"\n");
  96. if(user_year>3000)
  97. {
  98. r=r-1;
  99. }
  100. print_cal(r,last_day);
  101. }
  102. /*printing dates*/
  103. public void print_cal(int r,int last_day)
  104. {
  105. int i=0,day=1;
  106. System.out.println("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n\n");
  107. while(i<r)
  108. {
  109. System.out.print("\t*");
  110. i++;
  111. }
  112. for(i=r;i<7;i++)
  113. {
  114. System.out.print("\t"+day);
  115. day++;
  116. }
  117. while(day<=last_day)
  118. {
  119. System.out.println();
  120. for(i=1;i<=7;i++)
  121. { if(day==last_day+1)
  122. {
  123. break;
  124. }
  125. System.out.print("\t"+day);
  126. day++;
  127. }
  128. }
  129. System.out.println("\n\n\t***********************************************************");
  130. }
  131. }
  132. class main
  133. {
  134. public static void main(String args[])
  135. {
  136. calender obj=new calender();
  137. obj.display();
  138. }
  139. }


If don't understand any part of code than please feel free to comment.