get Number Of Months Apart Inclusive - Android java.util

Android examples for java.util:Month

Description

get Number Of Months Apart Inclusive

Demo Code


//package com.java2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static int getNumberOfMonthsApartInclusive(Date firstDate,
            Date lastDate) {/*  w w  w.  ja  v  a 2  s.c om*/
        Calendar firstCal = Calendar.getInstance();
        firstCal.setTime(firstDate);

        Calendar lastCal = Calendar.getInstance();
        lastCal.setTime(lastDate);

        return 12
                * (lastCal.get(Calendar.YEAR) - firstCal.get(Calendar.YEAR))
                + lastCal.get(Calendar.MONTH)
                - firstCal.get(Calendar.MONTH) + 1;
    }
}

Related Tutorials