Java Month Difference getDiffBetweenMonth(Date latestDate, Date current)

Here you can find the source of getDiffBetweenMonth(Date latestDate, Date current)

Description

get Diff Between Month

License

Apache License

Declaration

public static int getDiffBetweenMonth(Date latestDate, Date current) 

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 getDiffBetweenMonth(Date latestDate, Date current) {
        Calendar calCurrent = Calendar.getInstance();
        calCurrent.setTime(current);//from  w  w w .  jav  a  2s .  c om

        Calendar calLatest = Calendar.getInstance();
        calLatest.setTime(latestDate);

        if (calCurrent.get(Calendar.YEAR) - calLatest.get(Calendar.YEAR) > 0) {
            int diff = calCurrent.get(Calendar.YEAR) - calLatest.get(Calendar.YEAR);
            return (12 - (calLatest.get(Calendar.MONTH) + 1) + 1) + calCurrent.get(Calendar.MONTH)
                    + 12 * (diff - 1);
        } else {
            return calCurrent.get(Calendar.MONTH) - calLatest.get(Calendar.MONTH);
        }
    }
}

Related

  1. getDiffMonth(Date begin, Date end)
  2. getDiffMonth(Date start, Date end)
  3. getDiffMonths(Date date1, Date date2)