Author: kmorin Date: 2011-10-26 10:32:44 +0200 (Wed, 26 Oct 2011) New Revision: 2214 Url: http://nuiton.org/repositories/revision/nuiton-utils/2214 Log: add getDifferenceInYears Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/DateUtil.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/DateUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/DateUtil.java 2011-09-30 10:00:52 UTC (rev 2213) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/DateUtil.java 2011-10-26 08:32:44 UTC (rev 2214) @@ -355,6 +355,28 @@ } /** + * Do the difference between the two dates in argument. The result is a number + * of years between the two dates. + * Ex : 01/01/2009 and 28/02/2011 return 2 years. + * Warning, if beginDate is inferior to endDate, the result will be 1 minimum + * + * @param beginDate first date + * @param endDate second date + * @return a number of years between beginDate and endDate + */ + public static int getDifferenceInYears(Date beginDate, Date endDate) { + int count = 0; + Calendar fromCalendar = getDefaultCalendar(beginDate); + Calendar thruCalendar = getDefaultCalendar(endDate); + + while (fromCalendar.before(thruCalendar)) { + fromCalendar.add(Calendar.YEAR, 1); + count++; + } + return count; + } + + /** * Get libelle of the month corresponding to the number given in argument. * * @param monthNumber between 1-12
participants (1)
-
kmorin@users.nuiton.org