package com.forezp.util; import org.joda.time.DateTime; import org.joda.time.Months; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; /** * 在JAVA中,如何计算两个日期的月份差?<br> * * * @author Administrator * */ public class Demo1 { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd"); DateTime start = formatter.parseDateTime("2017-07-11"); DateTime end = formatter.parseDateTime("2017-11-19"); int months = Months.monthsBetween(start, end).getMonths(); System.out.println(months); } }
运行结果:
Maven导入依赖:
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9.6</version> </dependency>