用JAVA实现输入年月日,判断这是一年中的第几天、星期几(从1990年1-1日是星期一开始算)
在这里插入代码片
```static int outDay(int year,int mon, int day)//判断是哪一天的函数
{
int res=0;
switch(mon)
{
case 12:
res+=30;
case 11:
res=res+31;
case 10:
res=res+30;
case 9:
res=res+31;
case 8:
res=res+31;
case 7:
res=res+30;
case 6:
res=res+31;
case 5:
res=res+30;
case 4:
res=res+31;
case 3:
res=res+28;
case 2:
res=res+31;
case 1:
res=res+day;
}
if((year%400==0||year%4==0&&year%100!=0)&&mon>=3)//如果是闰年则天数+1,但是只有3月及以后才需要判断
res++;
return res;
}
static int allDays(int year)//判断某一年总天数的函数
{
int days=0;
if(year%400==0||year%4==0&&year%100!=0)
days=366;
else days=365;
return days;
}
static void outWeekday(int year,int mon, int day)//判断具体是星期几的函数
{
int sum=0;
for(int i=1990;i<year;i++)
{
sum=sum+allDays(i);
}
sum=sum+outDay(year,mon,day);//算出从1990-1-1到今天一共有多少天
switch((sum-1)%7)//千万不要忘记减1
{
case 0:
System.out.print("星期一");break;
case 1:
System.out.print("星期二");break;
case 2:
System.out.print("星期三");break;
case 3:
System.out.print("星期四");break;
case 4:
System.out.print("星期五");break;
case 5:
System.out.print("星期六");break;
case 6:
System.out.print("星期七");break;
}
}
//主函数调用
public static void main(String[] args) {
// TODO Auto-generated method stub
outWeekday(2022,1,11);
}