Commit cb104fa0 authored by hongguangwu's avatar hongguangwu

合同优化-年月差值

parent 556a631f
......@@ -861,15 +861,18 @@ public class DateUtil {
/**
* @param startDate
* @param endDate
* @Description: 计算年月的月份差值(202205-202205=0)想要1自己+1
* @Description: 计算年月的月份差值(202205-202205=0,202205-202105=12)想要1自己+1 年月差值
* @Author: hgw
* @Date: 2022/7/15 12:16
* @return: int
**/
public static int getMonthDiff(Date startDate, Date endDate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_YYYYMM);
return Integer.parseInt(sdf.format(endDate)) - Integer.parseInt(sdf.format(startDate));
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(startDate);
c2.setTime(endDate);
return 12 * (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
} catch (IllegalFormatException e) {
log.error("计算日期月份差,方法名:getMonthDiff,出错:",e);
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment