也可以计算两个日期之间相距多少天
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int year, month, day;
int ay, am, ad;
System.out.println("请输入你的出生日期:");
ay = sc.nextInt();
am = sc.nextInt();
ad = sc.nextInt();
System.out.println("请输入现在的年月日:");
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
int days = 0;
for (int m = am; m <= 12; m++) {
switch(m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days += 31;
break;
case 4:
case 6:
case 9:
case 11:
days += 30;
break;
case 2:
if(ay % 4 == 0 && ay % 100 != 0 || ay % 400 ==0) {
days += 29;
}else {
days += 28;
}
default: break;
}
}
days -= ad;
for (int y = ay+1; y < year; y++) {
if(y % 4 == 0 && y % 100 != 0 || y % 400 ==0) {
days += 366;
}else {
days += 365;
}
}
for (int m = 1; m < month; m++) {
switch(m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days += 31;
break;
case 4:
case 6:
case 9:
case 11:
days += 30;
break;
case 2:
if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0) {
days += 29;
}else {
days += 28;
}
default: break;
}
}
sc.close();
days = days + day;
System.out.println("相距" + days + "天");
}
}