计算某个日期是星期几java

计算某个日期是星期几?

package pac1;



import java.util.Scanner;

public class weekTest {
	public static boolean isLeap(int year){
		boolean leap=year%4==0||year%100!=0&&year%400==0;//判断是否为闰年
		return leap;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a=0,b=0;//a为闰年个数,b为平年个数
		Scanner sc=new Scanner(System.in);
		int year=sc.nextInt();//输入自定义年、月、日,求星期几
		int month=sc.nextInt();
		int day=sc.nextInt();

		
		//计算有多少个闰年和多少个平年
		for(int i=1980;i<year;i++){//以1980年1月1日星期二为起始点
			if(isLeap(i))
			{
				System.out.printf("%d是闰年%n",i);
				a++;
			}
			else{
				System.out.printf("%d是平年%n",i);
				b++;
			}
		}
		System.out.printf("闰年有%d个,",a);//a个闰年
		System.out.printf("平年有%d个",b);//b个平年

		int tian=a*2+b*1;//闰年+2,平年加1
		System.out.printf("%n年总天数有%d天%n",tian);

		
		int yue=0;//从2019.1月到year.month月一共有多少天
		for(int j=month-1;j>0;j--){
			switch(j){
			case 1:case 3:case 5:case 7:case 8:case 10:yue+=31;break;
			case 4:case 6:case 9:case 11:yue+=30;break;
			case 2:yue+=isLeap(year)?29:28;
			}
			
		}
		System.out.printf("从"+year+"年1月到"+year+"年"+month+"月一共有%d天%n",yue);
		System.out.println("本月过了"+day+"天");
		int total=tian+yue+day;
		System.out.printf("总天数=%d天%n", total);
		int week=1;
	    week=(week+total)%7;
		System.out.println(week);
		System.out.printf(year+"年"+month+"月"+day+"日     星期");
		switch(week)
		{
			case 0:System.out.println("日");break;
			case 1:System.out.println("一");break;
			case 2:System.out.println("二");break;
			case 3:System.out.println("三");break;
			case 4:System.out.println("四");break;
			case 5:System.out.println("五");break;
			case 6:System.out.println("六");break;
		
	}

}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值