問題描述
給定日期a"2019-04-21"和日期b"2018-12-21",計(jì)算兩個(gè)日期之間相差多少天橄碾?不允許使用高級(jí)封裝日期等卵沉。
解題思路
先處理字符串,變成輸入的年月日
再計(jì)算日期差
計(jì)算日期差思路:計(jì)算當(dāng)前日期a是相對(duì)于公元0年的總第多少天法牲,再計(jì)算日期b的總第多少天史汗,做差即可。
計(jì)算總第多少天思路:
1. 2019年之前總共有多少天:(2019-1)*365+閏年個(gè)數(shù)
2. 4月前 總共有多少天:平年4月前多少天 or 閏年4月前多少天
3. 總第天數(shù) = 前兩項(xiàng)+日期數(shù) 拒垃。
題解
/*
* Date : 2019.
* Author : Mereder
*/
public class theDateDiff {
public static class Mydate{
public int year;
public int mon;
public int day;
public Mydate(int year, int mon, int day) {
this.year = year;
this.mon = mon;
this.day = day;
}
}
public static final int daysOfMon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
public static int beforeMon_com[] = new int[13];
public static int beforeMon_leap[] = new int[13];
public static int total(Mydate mydate){
int total = beforeYear(mydate) + beforeMon(mydate) + mydate.day;
return total;
}
// 這一年之前一共多少天
public static int beforeYear(Mydate date){
int year = date.year;
// 被四整除的年份 減去 被100 整除的 (其中也減去了 被400 整除的 再加回來)
int total = (year-1)*365 + (year-1) / 4 - (year-1) / 100 + (year-1) / 400;
return total;
}
public static int beforeMon(Mydate mydate){
if (isLeapYear(mydate)){
return beforeMon_leap[mydate.mon];
}
else return beforeMon_com[mydate.mon];
}
public static boolean isLeapYear(Mydate mydate){
int year = mydate.year;
if ((year%4 == 0 && year%100 != 0) || (year%400 == 0)) return true;
else return false;
}
public static void main(String[] args) {
// 總體思路: 以2019年4月24日為例
// 2019年之前總共有多少天:(2019-1)*365+閏年個(gè)數(shù)
// 4月前 總共有多少天:平年4月前多少天 or 閏年四月前多少天
// 前兩項(xiàng)+24日 = 總共天數(shù)停撞。
// 輸入轉(zhuǎn)換
String date1 = "2019-04-24";
String date2 = "2019-04-22";
int n = 0;
// 初始化 月前總天數(shù)數(shù)組 含義:2月1日前總共多少天......12月1日前總共多少天
for (int i = 1; i < 13; i++) {
beforeMon_com[i] = n;
if (i > 2){
beforeMon_leap[i] = n +1;
}
else beforeMon_leap[i] = n;
n += daysOfMon[i];
}
int year1 = Integer.parseInt(date1.split("-")[0]);
int mon1 = Integer.parseInt(date1.split("-")[1]);
int day1 = Integer.parseInt(date1.split("-")[2]);
Mydate first = new Mydate(year1,mon1,day1);
int year2 = Integer.parseInt(date2.split("-")[0]);
int mon2 = Integer.parseInt(date2.split("-")[1]);
int day2 = Integer.parseInt(date2.split("-")[2]);
Mydate second = new Mydate(year2,mon2,day2);
System.out.printf("相差 %d 天",(total(first)-total(second)));
}
}