import java.util.Scanner;
public class first {
public static void main(String[] args) {
Scanner input_date = new Scanner(System.in);
System.out.print("请输入第一个日期:"); //小日期
String first_date = input_date.next();
System.out.print("请输入第二个日期:"); //大日期
String second_date = input_date.next();
String[] arr1 = first_date.split("/");
String[] arr2 = second_date.split("/");
int year1 = Integer.valueOf(arr1[0]);
int year2 = Integer.valueOf(arr2[0]);
int sum_days; //定义总共的天数。
if(year1 == year2){ //如果两个日期在同一年
sum_days = get_days(second_date) - get_days(first_date);
}
else{ //两日期不是同一年时
int leap = 0,not_leap = 0; //计算中间 润年和平年 各几年。
for (int i = year1 + 1; i < year2; i++){
if(leap_year(i) == 1){
leap ++;
}else {
not_leap ++;
}
}
int first_date_days = get_days(first_date); //计算第一个年所剩余的天数。first_date_days表示第一年之前的天数。
int days1 = 0;
if(leap_year(year1) == 1){
days1 = 366 - first_date_days;
}else{
days1 = 365 - first_date_days;
}
int days2 = get_days(second_date); //计算最后一年的天数。
sum_days = days1 + days2 + leap*366 + not_leap*365;
}
System.out.println("两个日期之间相差:"+sum_days +"天");
}
public static int get_days(String str){ //获取这个日期在这一年的第几天
String[] arr = str.split("/");
int year = leap_year(Integer.valueOf(arr[0]));
int month = Integer.valueOf(arr[1]);
int day = Integer.valueOf(arr[2]);
int days = 0;
switch (month) {
case 1:
days = day;
break;
case 2:
days = 31 + day;
break;
case 3:
days = 31 + 29 + day;
break;
case 4:
days = 31 + 29 + 31 + day;
break;
case 5:
days = 30 + 29 + 31 + 30 + day;
break;
case 6:
days = 30 + 29 + 31 + 30 + 31 + day;
break;
case 7:
days = 30 + 29 + 31 + 30 + 31 + 30 + day;
break;
case 8:
days = 30 + 29 + 31 + 30 + 31 + 30 + 31 + day;
break;
case 9:
days = 30 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day;
break;
case 10:
days = 30 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
break;
case 11:
days = 30 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
break;
case 12:
days = 30 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
break;
default:
break;
}
if (year != 1 && month > 2) {
days = days - 1;
}
return days;
}
private static int leap_year(int year) { //判断润平年,闰年返回1 平年返回0
boolean flag = false;
if(year%4 == 0 && year%100 != 0){
flag = true;
}
if (year%400 == 0){
flag = true;
}
if(flag){
return 1;
}else{
return 0;
}
}
}