题目描述:
1360. 日期之间隔几天 - 力扣(LeetCode) (leetcode-cn.com)
Java代码:
?
class Solution {
public boolean isGap(int y){return y%100==0?y%400==0:y%4==0;}
public int cal(String s){//1971-01-01
int[] iymd=new int[4],ms={0,31,28,31,30,31,30,31,31,30,31,30,31};
for(String e:s.split("-"))iymd[++iymd[0]]=Integer.parseInt(e);
for(int y=iymd[1]-1;y>=1971;y--)iymd[3]+=isGap(y)?366:365;
for(int m=iymd[2]-1;m>0;m--)iymd[3]+=ms[m];
return iymd[2]>2&&isGap(iymd[1])?iymd[3]+1:iymd[3];
}
public int daysBetweenDates(String a, String b) {
return Math.abs(cal(a)-cal(b));
}
}
?
|