Java Month Difference getDiffMonths(Date date1, Date date2)

Here you can find the source of getDiffMonths(Date date1, Date date2)

Description

get Diff Months

License

Open Source License

Declaration

public static int getDiffMonths(Date date1, Date date2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;

public class Main {

    public static int getDiffMonths(Date date1, Date date2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);//from   w w w. j av  a 2 s.c om
        if (date2 == null)
            c2.setTime(new java.util.Date());
        else
            c2.setTime(date2);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);

        return (year2 - year1) * 12 + month2 - month1 + 1;
    }
}

Related

  1. getDiffBetweenMonth(Date latestDate, Date current)
  2. getDiffMonth(Date begin, Date end)
  3. getDiffMonth(Date start, Date end)