1 public class 计算两个日期差 { 2 // ********************计算两个日期差******************* 3 static boolean if_run(int year) {// 判断是否为闰年函数 4 boolean get = false; 5 if (year % 4 == 0) 6 get = true; 7 if (year % 100 == 0) 8 get = false; 9 if (year % 400 == 0) 10 get = true; 11 return get; 12 } 13 14 static int get_days(int year, int month, int day) {// 计算与基点日期的差 15 int sum = 0; 16 int[] a = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 17 if (if_run(year)) 18 a[2] += 1; 19 for (int i = 1; i < year; i++) {// 计算与基点日期差几年就加上相应的的天数 20 sum = sum + 365; 21 if (if_run(i)) 22 sum++; 23 24 } 25 for (int i = 1; i < month; i++) {// 计算与基点日期差几月就加上相应的的天数 26 sum = sum + a[i]; 27 } 28 sum = sum + day;// 计算与基点日期差几天就加上相应的的天数 29 return sum; 30 31 } 32 33 static int day_dif(int year1, int month1, int day1, int year2, int month2, int day2) { 34 35 return get_days(year2, month2, day2) - get_days(year1, month1, day1); 36 37 } 38 39 public static void main(String[] args) { 40 int day = day_dif(1996, 11, 28, 2020, 1, 1); 41 System.out.println(day); 42 } 43 44 }