计算java中两个日期之间的月份

时间:2013-08-26 08:46:29

标签: java

我需要计算两个日期之间的月份, 如果startDate = 2013.01.01,endDate = 2013.01.31答案应为1, startDate = 2013.01.01,endDate = 2013.02.01答案应为2。 请帮忙

1 个答案:

答案 0 :(得分:8)

使用Joda Months

DateTime start = new DateTime(startDate.getTime());
DateTime end= new DateTime(endDate.getTime());
int months = Months.monthBetween(start, end).getMonths();