Get number of month difference with the current month - Android java.util

Android examples for java.util:Month

Description

Get number of month difference with the current month

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    /**/*from   www  . j  a  v a2 s.c om*/
     * Get number of month difference with the current month
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDifference(int year, int month) {
        Calendar cal = Calendar.getInstance();
        int currentMonth = cal.get(Calendar.MONTH);
        int currentYear = cal.get(Calendar.YEAR);

        return (currentYear - year) * 12 + currentMonth - month;
    }

    /**
     * Get number of month difference between two the start and end month/year
     * @param startYear
     * @param startMonth
     * @param endYear
     * @param endMonth
     * @return
     */
    public static int getMonthDifference(int startYear, int startMonth,
            int endYear, int endMonth) {
        return (endYear - startYear) * 12 + endMonth - startMonth;
    }
}

Related Tutorials