Complete source code below will show you, how to create simple calendar in java. This program will prompts the user to enter the year and first day of the year, and display the calendar table for the year on the console.
*************************************************************************
COMPLETE SOURCE CODE FOR : JavaSimpleCalendar.java
*************************************************************************
import java.util.Calendar;
import java.util.Scanner;
public class JavaSimpleCalendar
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.print("YEAR : ");
int year=s.nextInt();
System.out.println();
System.out.println("FIRST DAY OF THE YEAR");
System.out.println("1 for MONDAY");
System.out.println("2 for TUESDAY");
System.out.println("3 for WEDNESDAY");
System.out.println("4 for THURSDAY");
System.out.println("5 for FRIDAY");
System.out.println("6 for SATURDAY");
System.out.println("7 for SUNDAY");
System.out.print("FIRST DAY : ");
int firstDay=s.nextInt();
System.out.println();
boolean leapYear=false;
if(year%4==0)
{
leapYear=true;
}
for(int i=1;i<=12;i++)
{
System.out.println("***********************");
System.out.println("MONTH : "+i);
System.out.println("***********************");
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
System.out.println("M T W TH F S SU");
boolean firstRound=true;
for(int j=1;j<=31;j++)
{
String temp=Integer.toString(j);
if(temp.length()==1)
{
System.out.print(" ");
}
if(firstDay==1)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=1;k++)
{
System.out.print("");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==2)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=3;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==3)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=6;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==4)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=9;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==5)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=12;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==6)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=15;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==7)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=18;k++)
{
System.out.print("-");
}
System.out.println(j+" ");
}
else
{
System.out.println(j+" ");
}
firstDay=0;
firstRound=false;
}
firstDay++;
}
System.out.println("\n");
}
else if(i==2)
{
if(leapYear==true)
{
System.out.println("M T W TH F S SU");
boolean firstRound=true;
for(int j=1;j<=29;j++)
{
String temp=Integer.toString(j);
if(temp.length()==1)
{
System.out.print(" ");
}
if(firstDay==1)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=1;k++)
{
System.out.print("");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==2)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=3;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==3)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=6;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==4)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=9;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==5)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=12;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==6)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=15;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==7)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=18;k++)
{
System.out.print("-");
}
System.out.println(j+" ");
}
else
{
System.out.println(j+" ");
}
firstDay=0;
firstRound=false;
}
firstDay++;
}
System.out.println("\n");
}
else
{
System.out.println("M T W TH F S SU");
boolean firstRound=true;
for(int j=1;j<=28;j++)
{
String temp=Integer.toString(j);
if(temp.length()==1)
{
System.out.print(" ");
}
if(firstDay==1)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=1;k++)
{
System.out.print("");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==2)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=3;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==3)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=6;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==4)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=9;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==5)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=12;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==6)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=15;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==7)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=18;k++)
{
System.out.print("-");
}
System.out.println(j+" ");
}
else
{
System.out.println(j+" ");
}
firstDay=0;
firstRound=false;
}
firstDay++;
}
System.out.println("\n");
}
}
else
{
System.out.println("M T W TH F S SU");
boolean firstRound=true;
for(int j=1;j<=30;j++)
{
String temp=Integer.toString(j);
if(temp.length()==1)
{
System.out.print(" ");
}
if(firstDay==1)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=1;k++)
{
System.out.print("");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==2)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=3;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==3)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=6;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==4)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=9;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==5)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=12;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==6)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=15;k++)
{
System.out.print("-");
}
System.out.print(j+" ");
}
else
{
System.out.print(j+" ");
}
}
else if(firstDay==7)
{
if(firstRound==true&&j==1)
{
for(int k=1;k<=18;k++)
{
System.out.print("-");
}
System.out.println(j+" ");
}
else
{
System.out.println(j+" ");
}
firstDay=0;
firstRound=false;
}
firstDay++;
}
System.out.println("\n");
}
}
}
}
*************************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************************
The Program Runs Correctly If we remove word "public" from sentence
ReplyDelete"public class JavaSimpleCalender"
Only "class JavaSimpleCalender" works
good program
umm.. I would like to ask if you know how to get the remaining days and passed days of the given year.?
ReplyDeleteCan you help me..? Please I really need your help..,
ReplyDeleteIn Programming Exercise 2, the class Date was designed and implemented to keep track of a date, but it has a very limited operations. Redefine the class Date so that, in addition to the operations already defined, it can perform the following operations on a date:
a. Set the month.
b. Set the day.
c. Set the year.
d. Return the month.
e. Return the day.
f. Return the year.
g. Test whether the year is a leap year.
h. Return the number of days in the month. For example, if the date is 3-12-2005, the number of days to be returned is 31 because there are 31 days in March.
i. Return the number of days passed in the year. For example, if the date is 3-18-2005, the number of days passed in the year is 77. Note that the number of days returned also includes the current day.
j. Return the number of days remaining in the year. For example, if the date is 3-18-2005, the number of days remaining in the year is 288.
k. Calculate the new date by adding a fixed number of days to the date. For example, if the date is 3-18-2005 and the days to be added are 25, the new date is 4-12-2005.
please email me at Mikee_mm16@yahoo.com I really need your help. :(
ReplyDelete