diff --git a/src/java/com/samskivert/util/CalendarUtil.java b/src/java/com/samskivert/util/CalendarUtil.java index 4da86768..1f2b9d5d 100644 --- a/src/java/com/samskivert/util/CalendarUtil.java +++ b/src/java/com/samskivert/util/CalendarUtil.java @@ -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. * *
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; + } }