use switch code..
refer to this.. a simple calendar
public class MyCalendar
{
private final int newYear1901=2;//Jan. 1, 1901, Tue(2)
private int month;
private int year;
public MyCalendar(int month, int year)
{
this.month=month;
this.year=year;
}
public MyCalendar()
{
this(1,2010);
}
public int newYearDay(int year)
{
int elapseYear=year-1901;
int countLeapYear=(elapseYear)/4;
return(newYear1901+elapseYear+countLeapYear)%7;
}
public boolean isLeapYear(int year)
{
return (year%4)==0;
}
public int monthDays(int month, int year)
{
int days=-1;
switch(month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days=31;break;
case 4: case 6: case 9: case 11:
days=30;break;
case 2:
days=(isLeapYear(year))?29:28;
}
return days;
}
public int firstDayMonth(int month, int year)
{
int newYear=newYearDay(year);
int totalDays=0;
for(int i=1;i<month;i++)
{
totalDays+=monthDays(i,year);
}
return(newYear+totalDays)%7;
}
public String monthName(int month)
{
String name="";
switch(month)
{
case 1:name="January"; break;
case 2:name="February"; break;
case 3:name="March"; break;
case 4:name="April"; break;
case 5:name="May"; break;
case 6:name="June"; break;
case 7:name="July"; break;
case 8:name="August"; break;
case 9:name="September"; break;
case 10:name="Ocotber"; break;
case 11:name="November"; break;
case 12:name="December"; break;
}
return name;
}
public void displayChart()
{
String[]dayNames={"SUN","MON","TUE","WED","THU","FRI","SAT"};
System.out.printf("\n\n%s\t%d\n", monthName(month),year);
System.out.println("----------------------------------------------------");
for(String d:dayNames)
System.out.print(d+"\t");
System.out.println("\n----------------------------------------------------");
int i=0;
for(;i<firstDayMonth(month,year);i++)
System.out.print("\t");
for(int j=1;(i<42 && j<=monthDays(month,year));i++,j++)
{
if((i%7)==0)
System.out.printf("\n%3d\t",j);
else
System.out.printf("%3d\t",j);
}
System.out.println("\n----------------------------------------------------");
}
public static void main(String[]args)
{
try
{
System.out.print("YEAR: ");
int year=new java.util.Scanner(System.in).nextInt();
System.out.print("MONTH: ");
int month=new java.util.Scanner(System.in).nextInt();
MyCalendar m= new MyCalendar(month,year);
m.displayChart();
}
catch(Exception e)
{
System.out.println("INVALID INPUT..Enter Number");
System.out.print("YEAR: ");
int year=new java.util.Scanner(System.in).nextInt();
System.out.print("MONTH: ");
int month=new java.util.Scanner(System.in).nextInt();
MyCalendar m= new MyCalendar(month,year);
m.displayChart();
}
}
}//end of class
i hope it helps..