From 647a4a597d67d4329f7e8c10881201df0c069f28 Mon Sep 17 00:00:00 2001 From: mdb Date: Mon, 6 Mar 2006 23:25:02 +0000 Subject: [PATCH] Added a couple of date calculation methods. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1794 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/CalendarUtil.java | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) 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; + } }