Ordinary write logic to determine the difference between the two month time required to write a lot of logic, such as the comparison year, so is there any way you can quickly calculate it?
The introduction of dependence:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
Test code:
public class MyTest {
public static void main(String[] args) {
//当前时间
String formatDate =DateFormat.getDateInstance().format(new Date());
System.out.println("当前系统时间="+formatDate);
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime start = formatter.parseDateTime("2019-11-11");
DateTime end = formatter.parseDateTime(formatDate);
System.out.println("开始时间="+start);
System.out.println("结束时间="+end);
//end-start 结果可为负数、正数、0
int months = Months.monthsBetween(start, end).getMonths();
//取绝对值
System.out.println("结束时间-开始时间="+Math.abs(months)+"(月)");
System.out.println(months);
}
}
Console output:
He said the last words: it is not easy to write, if you like, or to help remember the thumbs up or the collection + attention Oh ~