Skip to content

Instantly share code, notes, and snippets.

@bittib
Created June 22, 2013 17:39
Show Gist options
  • Save bittib/5841760 to your computer and use it in GitHub Desktop.
Save bittib/5841760 to your computer and use it in GitHub Desktop.
Diff Days
static int daysSinceYearStart(int y, int m, int d, boolean flag){
boolean leapYear = isLeapYear(y);
int total = leapYear ? 366 : 365;
int days = d;
days += DayOfMonth[m-1];
if (leapYear && m > 2)
days++;
return flag ? days : total - days;
}
static int dateDiff(int y1, int m1, int d1, int y2, int m2, int d2){
if (y1 > y2 || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2))
return dateDiff(y2, m2, d2, y1, m1, d1);
int days = 0;
if (y1 == y2){
if (m1 == m2){
return d2 - d1;
}else{// m1 < m2
return (DayOfMonth[m2-1] + d2) - (DayOfMonth[m1-1] + d1);
}
}
// y1 < y2
days += daysSinceYearStart(y1, m1, d1, false);
for (int i = y1+1; i < y2; i++){
days += isLeapYear(i) ? 366 : 365;
}
return days + daysSinceYearStart(y2, m2, d2, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment