获取某年某月某日是周几

public static void getweek(){

/*
遇到整百年时(如2000,1900,300)要被400整除才是闰年,否则为平年
(2000闰年,1900平年,300平年)
;遇到非整百年时(如2004,2005),只要被4整除就是闰年,不能被4整除为平年
(2004闰年,2005平年)。
闰年的2月有29天,平年的2月有28天。
*/
Scanner scan = new Scanner(System.in);
int allday = 0;//总天数
System.out.println("请输入年份:");
int year = scan.nextInt();//年
System.out.println("请输入月份:");
int month = scan.nextInt();//月
System.out.println("请输入日期:");
int day = scan.nextInt();//日
if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) {
System.out.println(year + " 是闰年!");
}else {
System.out.println(year + " 是平年!");
}
for (int i = 1900; i < year; i++) {
if ((i % 100 != 0 && i % 4 == 0) || (i % 100 == 0 && i % 400 == 0)) {
allday += 366;
}else {
allday += 365;
}
}
for (int i = 1; i < month; i++) {
switch (i) {
case 4:
case 6:
case 9:
case 11:
allday += 30;
break;
case 2:
if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) {
allday += 29;
}else {
allday += 28;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
allday += 31;
break;
}
}
allday = allday + day;
System.out.println(year + "年" + month + "月" + day + "日距 1900年1月1日 共 " + allday + " 天!");
int week = allday % 7;
System.out.println("周" + week);
}

 

/***********运行如下************/

请输入年份:
2016
请输入月份:
10
请输入日期:
3
2016 是闰年!
2016年10月3日距 1900年1月1日 共 42645 天!
周1

转载于:https://www.cnblogs.com/weloglog888/p/5929148.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值