Calculate difference in months between two dates with Groovy

date, datediff, groovy, time

Solution

monthBetween = (start[Calendar.MONTH] - end[Calendar.MONTH]) + 1
yearsBetween = start[Calendar.YEAR] - end[Calendar.YEAR]
months = monthBetween + (yearsBetween * 12)

Problem

I need to calculate the difference in months between two dates. ``` start = new Date(112, 4, 30) // Wed May 30 00:00:00 CEST 2012 end = new Date(111, 9, 11) // Tue Oct 11 00:00:00 CEST 2011 assert 8 == monthsBetween(start, end) ``` Using Joda-Time it's really easy to achieve this with something like this: ``` months = Months.monthsBetween(start, end).getMonths() ``` How can I achieve this in a Groovy way, without using other libraries?

Original source