package pwd;
/**
* 程序员必做的50道题之4:输入某年某月某日,判断这一天是这一年的第几天?
* <p>@author SunJian</p>
* <p>date:2017年10月20日</p>
* <p>QQ:330160114</p>
* <p>company:四海兴唐</p>
* <p>site:www.ccshxt.com</p>
*/
public class DateCount {
public static void main(String[] args) {
//此处可以改为键盘输入
String input="20171020";
//一年中的第多少天
int howManyDates=0;
//解析出年、月、日
int year=Integer.parseInt(input.substring(0,4));
int month=Integer.parseInt(input.substring(4,6));
int date=Integer.parseInt(input.substring(6,8));
boolean run=false;
//判断是否是闰年
if((year % 100 ==0 && year % 4==0) && year % 400 !=0){
run=true;
}
for(int m=1;m<month;m++){
switch(m){
case 1:
howManyDates+=31;
break;
case 2:
if(run){
//闰年,二月29天
howManyDates+=29;
}else{
//非闰年,二月28天
howManyDates+=28;
}
break;
case 3:
howManyDates+=31;
break;
case 4:
howManyDates+=30;
break;
case 5:
howManyDates+=31;
break;
case 6:
howManyDates+=30;
break;
case 7:
howManyDates+=31;
break;
case 8:
howManyDates+=31;
break;
case 9:
howManyDates+=30;
break;
case 10:
howManyDates+=31;
break;
case 11:
howManyDates+=30;
break;
case 12:
howManyDates+=31;
break;
default:
System.out.println("错误");
}
}
howManyDates=howManyDates+date;
System.out.println(input+"是"+year+"年的第"+howManyDates+"天");
}
}