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:
@@ -22,11 +22,20 @@ public class CalendarUtil
|
|||||||
cal.set(Calendar.MILLISECOND, 0);
|
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
|
* Returns the difference between the dates represented by the two
|
||||||
* calendars in days, properly accounting for daylight savings time,
|
* calendars in days, properly accounting for daylight savings time, leap
|
||||||
* leap seconds, etc. The order of the two dates in time does not
|
* seconds, etc. The order of the two dates in time does not matter, the
|
||||||
* matter, the absolute number of days between them will be returned.
|
* absolute number of days between them will be returned.
|
||||||
*
|
*
|
||||||
* <p> From: http://www.jguru.com/forums/view.jsp?EID=489372
|
* <p> From: http://www.jguru.com/forums/view.jsp?EID=489372
|
||||||
*
|
*
|
||||||
@@ -52,4 +61,30 @@ public class CalendarUtil
|
|||||||
}
|
}
|
||||||
return days;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user