import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @ClassName test
* @Description 测试类
* @Author asus
* @Date 2020/12/15 16:02
* @Version 1.0
**/
public class Main{
/**
* @return
* @Description 在控制台输入一个日期,判断这个日期是这一年的第几天?
* @Author guojingting
* @Date 2020/12/15 16:03
* @Version 1.0
* @Parm
**/
public static void main(String[] args) throws IOException {
//输入这个年份的日期
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String date = bufferedReader.readLine();//例如:20201215 20200605
int year = Integer.parseInt(date.substring(0, 4));
//判断这个年份是否为闰年
int february;//二月
if (year % 4 == 0 || year % 100 == 0 || year % 400 == 0) {
//如果是闰年,那么february=29,否则february=28
february = 29;
} else {
february = 28;
}
//转换月份,如果月份中有0,那么把0去掉
String monthStr = date.substring(4, 6);
int month = 0;
if (monthStr.charAt(0)=='0'){
month = Integer.parseInt(monthStr.substring(0));
}
//转换day,如果day有0,那么把0去掉
String dayStr = date.substring(6, 8);
int day = 0;
if (dayStr.charAt(0) == '0') {
day = Integer.parseInt(dayStr.substring(0));
}
int sum=0;//总天数是多少 31 28/29 31 30 31 30 31 31 30 31 30 31
switch (month-1){
case 11:sum+=30;
case 10:sum+=31;
case 9:sum+=30;
case 8:sum+=31;
case 7:sum+=31;
case 6:sum+=30;
case 5:sum+=31;
case 4:sum+=30;
case 3:sum+=31;
case 2:sum+=february;
case 1:sum+=31;
}
sum+=day;
System.out.println("年:"+year+"月:"+"天"+day);
System.out.println("天数为"+sum+"天");
}
}
还没有评论,来说两句吧...