Java Month Difference getDiffMonth(Date begin, Date end)

Here you can find the source of getDiffMonth(Date begin, Date end)

Description

get Diff Month

License

Apache License

Declaration

public static int getDiffMonth(Date begin, Date end) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

import java.util.Date;

public class Main {
    public static int getDiffMonth(Date begin, Date end) {
        Calendar bc = Calendar.getInstance();
        bc.setTimeInMillis(begin.getTime());
        //bc.setTime(begin.getTime());
        Calendar be = Calendar.getInstance();
        be.setTime(end);//from   w  ww  . ja v  a 2  s  .  c  om
        int beginYear = bc.get(Calendar.YEAR);
        int beginMonth = bc.get(Calendar.MONTH);

        int endYear = be.get(Calendar.YEAR);
        int endMonth = be.get(Calendar.MONTH);

        int difMonth = (endYear - beginYear) * 12 + (endMonth - beginMonth);

        return difMonth;
    }
}

Related

  1. getDiffBetweenMonth(Date latestDate, Date current)
  2. getDiffMonth(Date start, Date end)
  3. getDiffMonths(Date date1, Date date2)