我需要计算两个日期之间的月差。
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)
使用 Joda-Time用这样的东西很容易实现这一点:
months = Months.monthsBetween(start, end).getMonths()
如何在不使用其他库的情况下以 Groovy 方式实现这一点?
最佳答案
monthBetween = (start[Calendar.MONTH] - end[Calendar.MONTH]) + 1
yearsBetween = start[Calendar.YEAR] - end[Calendar.YEAR]
months = monthBetween + (yearsBetween * 12)
关于date - 使用 Groovy 计算两个日期之间的月差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10820022/