import java.awt.*;
import java.awt.event.*;
public class Calendar extends Applet implements ActionListener
{
Label l,l2;
TextField t1,t2;
Button b;
TextArea ta;
public void init()
{
//setLayout(new GridLayout(4,4));
l=new Label("Enter Year ");
t1=new TextField(20);
l2=new Label("Enter Month ");
t2=new TextField(20);
b=new Button("Click Here To Display Calendar");
ta=new TextArea(10,55);
add(l);
add(t1);
add(l2);
add(t2);
add(b);
add(ta);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
int y=Integer.parseInt(t1.getText());
int m=Integer.parseInt(t2.getText());
ta.setText("");
ta.append("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
calc(y,m);
}
catch(Exception e)
{
ta.append("\t\tWrong Input OR No Input\n\n\t\tPlease Enter Correct MOnth and Year");
}
}
public void calc(int year,int month)
{
int p400,pr,odd=0,leapyr,seek=8,ordyr,totday=0,oddaft;
int jan=31,feb=0,mar=31,apr=30,may=31,jun=30,jul=31,aug=31,sep=30,oct=31,nov=30;
p400=(year-1)%400;
if(p400>300)
{
pr=p400%300;
odd=odd+1;
}
else if(p400>200)
{
pr=p400%200;
odd=odd+3;
}
else if(p400>100)
{
pr=p400%100;
odd=odd+5;
}
else
{
pr=p400;
}
leapyr=pr/4;
ordyr=pr-leapyr;
odd=odd+(leapyr*2)+ordyr;
odd=odd%7;
if(year%4==0)
{
feb=29;
}
else
{
feb=28;
}
switch(month)
{
case 1:
totday=0;
break;
case 2:
totday=jan;
break;
case 3:
totday=jan+feb;
break;
case 4:
totday=jan+feb+mar;
break;
case 5:
totday=jan+feb+mar+apr;
break;
case 6:
totday=jan+feb+mar+apr+may;
break;
case 7:
totday=jan+feb+mar+apr+may+jun;
break;
case 8:
totday=jan+feb+mar+apr+may+jun+jul;
break;
case 9:
totday=jan+feb+mar+apr+may+jun+jul+aug;
break;
case 10:
totday=jan+feb+mar+apr+may+jun+jul+aug+sep;
break;
case 11:
totday=jan+feb+mar+apr+may+jun+jul+aug+sep+oct;
break;
case 12:
totday=jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov;
break;
}
oddaft=totday %7;
odd=(odd+oddaft)%7;
odd=odd+1;
for(int j=0;j
ta.append("\t");
}
for(int i=1;i<32;i++)
{
if(i==(seek-odd) || i==(seek+7-odd) || i==(seek+7+7-odd) || i==(seek+7+14-odd) || i==(seek+7+21-odd))
{
ta.append("\n"+"\t"+i);
}
else
{
ta.append("\t"+i);
}
}
}
}
/**/

No comments:
Post a Comment