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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18440083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:38:28  来源:igfitidea点击:

calculate months between two dates in java

java

提问by user2717410

i need to calculate months between two dates, if the startDate=2013.01.01, endDate=2013.01.31 the answer should be 1, startDate=2013.01.01, endDate=2013.02.01 the answer should be 2. please help

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

回答by Jean Logeart

Use Joda Months:

使用 Joda月份

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