package try4;
public class CaculatorDays {
public static int CaculatorDays (int year,int month)
{
return getWeeks( year,month);
}
public static int CaculatorDays(int year,int month,int k)
{
return getTheMonthDays(year,month);
}
//计算今年到1900年有多少天
static long getyearDays(int year) {
long yearDays = 0;
int yuannian = 1900;//以1900年为计算基础
for (; year>yuannian;yuannian++)
{
if (isRun(yuannian) == true)
{
yearDays+= 366;
}
else
{
yearDays+= 365;
}
}
return yearDays;
}
//判断是否是闰年
static boolean isRun(int y) {
if((y%4==0&&y%100!=0)||(y%400==0))
{
return true;
}
else
{
return false;
}
}
//计算当前月份大1月1日有多少天
public static long getmonthDays(int year,int month)
{
long monthDays=0;
for(int i=1;i<month;i++)
{
switch(i)
{
case 4:
case 6:
case 9:
case 11:
monthDays+=30;break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDays+=31;break;
case 2:
if(isRun(year)==true)
{
monthDays+=29;break;
}
else
{
monthDays+=28;break;
}
}
}
return (monthDays);
}
//得到润平年每月的天数,用于显示当月月历
public static int getTheMonthDays(int year,int month)
{
int TheMonthDays;
int []Pyuefen={31,28,31,30,31,30,31,31,30,31,30,31};
int []Ryuefen={31,29,31,30,31,30,31,31,30,31,30,31};
if(isRun(year))
{
TheMonthDays=Ryuefen[month-1];
}
else
{
TheMonthDays=Pyuefen[month-1];
}
return TheMonthDays;
}
//得到当前星期几
public static int getWeeks(int year,int month)
{
long Alldays=getmonthDays(year,month)+getyearDays(year);
int Week=(int)Alldays%7;
Week = Week+1;
if(Week==7)
{
Week = 0;
}
return Week;
}
}
|
|