program to print calendar in java

import java.util.*;
class Calendar
{
public static void main(String ar[])
{
int a=0,E=0,A=0,D=0;
int end=0;
int t=1;
char y[]=new char[4];
int year[]=new int[4];
int c[]=new int[2];
 System.out.println("enter any date ");
Scanner in=new Scanner(System.in);
int d=in.nextInt();//day
int m=in.nextInt();//month
in.nextLine();
String yy=in.nextLine();//year
System.out.println(d+"/"+m+"/"+yy);
int B=Integer.parseInt(yy);
c[0]=B/100;
 c[1]=B%100;
int dd=d%7;
if ((B%4==0)&&(m==2))
{
 end=29;
}
else if(m==2)
 end=28;
switch(m)
{
 case 1:a=0;
        end=31;
        break;
 case 2:a=3;
        break;
 case 3:a=3;
        end=31;
        break;
 case 4:a=6;
        end=30;
        break;
 case 5:a=1;
        end=31;
        break;
 case 6:a=4;
        end=30;
        break;
 case 7:a=6;
        end=31;
        break;
 case 8:
        a=2;
        end=30;
        break;
 case 9:a=5;
        end=31;
        break;
 case 10:a=0;
         end=30;
         break;
 case 11:a=3;
         end=31;
         break;
 case 12:a=5;
         end=30;
         break;
 default :
    System.out.println("wrong month");
}
int x=c[0]/4;
x++;
x*=4;int C=0;
 A=((x-1)-c[0])*2;
if(m==1||m==2 && B%4==0)
{
 C=((c[1]+(c[1]/4))%7)-1;
 }
else
 { C=(c[1]+(c[1]/4))%7;
}
 D=(dd+a+A+C)%7;//offset value of day
 switch(D)
 {  case 0:
        System.out.println("sunday");
  break;
    case 1:
     System.out.println("monday");
  break;
 case 2:
  System.out.println("tuesday");
  break;
 case 3:
  System.out.println("wednesday");
  break;
 case 4:
  System.out.println("thursday");
  break;
 case 5:
  System.out.println("friday");
  break;
 case 6:
  System.out.println("saturday");
  break;
 default :
        System.out.println("you have given wrong date");
  break;
 }
 System.out.println("  CALENDAR OF ENTERED MONTH"+m"  AND "+B+"year");
 System.out.println("  _______________________________________");
 System.out.println("sun    mon    tue    wed    thurs  fri   sat");
 E=(1+a+A+C)%7;    
 switch(E)
 {
case 0:System.out.print(" 1     ");
           break;
  case 1:
   System.out.print("\t1     ");
   break;
  case 2:
   System.out.print("\t\t1     ");
   break;
  case 3:
   System.out.print("\t\t\t1     ");
   break;
  case 4:
   System.out.print("\t\t\t\t1     ");
   break;
  case 5:
   System.out.print("\t\t\t\t\t1     ");
   break;
  case 6:
   System.out.print("\t\t\t\t\t\t1     ");
   break;
 }
 int i=2;
 for(i=2;i<=(7-E);i++)
 {
  System.out.print(" "+i+"     ");
 }
 System.out.println();
 for(int j=i;j<=end;j++)
 {

  if(t==8)
  {
   System.out.println();
   t=1;
  }
if(j==1||j==2||j==3||j==4||j==5||j==6||j==7||j==8||j==9)
   System.out.print(" "+j+"     ");
else
  System.out.print(j+"     ");
t++;
 }
 }
}
                                  OUTPUT
  
enter any date 
22/5/2017
monday
  CALENDAR OF ENTERED MONTH 5 AND 2017year
sun       mon      tue      wed       thurs      fri      sat
                1         2           3          4          5          6
  7            8         9         10        11        12        13
14          15       16         17        18        19        20     
21          22       23         24        25        26        27     
28          29       30         31     
  

Comments