Added a couple of date calculation methods.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1794 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2006-03-06 23:25:02 +00:00
parent fde58fe833
commit 647a4a597d
+38 -3
View File
@@ -22,11 +22,20 @@ public class CalendarUtil
cal.set(Calendar.MILLISECOND, 0);
}
/**
* Returns the absolute difference between two longs, which have the
* significance of acting as miliseconds since the Epoch.
*/
public static long getTimeBetween (long start, long end)
{
return Math.abs(start - end);
}
/**
* Returns the difference between the dates represented by the two
* calendars in days, properly accounting for daylight savings time,
* leap seconds, etc. The order of the two dates in time does not
* matter, the absolute number of days between them will be returned.
* calendars in days, properly accounting for daylight savings time, leap
* seconds, etc. The order of the two dates in time does not matter, the
* absolute number of days between them will be returned.
*
* <p> From: http://www.jguru.com/forums/view.jsp?EID=489372
*
@@ -52,4 +61,30 @@ public class CalendarUtil
}
return days;
}
/**
* Returns the number of whole months between the dates represented by the
* two calendar objects, truncating any remainder. The order of the two
* dates in time does not matter, the absolute number of months between
* them will be returned.
*/
public static int getMonthsBetween (Calendar start, Calendar end)
{
if (end.before(start)) {
Calendar swap = start;
start = end;
end = swap;
}
// we're going to manipulate end, so let's clone it
end = (Calendar)end.clone();
int months = 0;
do {
end.add(Calendar.MONTH, -1);
months++;
} while (start.before(end));
return months;
}
}