Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.Calendar;

public class Main {
    /**
     * 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;
    }
}