Java Date to Month getMonths(Date date1, Date date2)

Here you can find the source of getMonths(Date date1, Date date2)

Description

get Months

License

Open Source License

Declaration

public static int getMonths(Date date1, Date date2) 

Method Source Code

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

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static int getMonths(Date date1, Date date2) {
        int iMonth = 0;
        try {/*  w  ww .  j  a v  a  2  s. c o  m*/
            Calendar objCalendarDate1 = Calendar.getInstance();
            objCalendarDate1.setTime(date1);
            Calendar objCalendarDate2 = Calendar.getInstance();
            objCalendarDate2.setTime(date2);
            if (objCalendarDate2.equals(objCalendarDate1))
                return 0;
            if (objCalendarDate1.after(objCalendarDate2)) {
                Calendar temp = objCalendarDate1;
                objCalendarDate1 = objCalendarDate2;
                objCalendarDate2 = temp;
            }
            if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR))
                iMonth = (objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR)) * 12
                        + objCalendarDate2.get(Calendar.MONTH) - objCalendarDate1.get(Calendar.MONTH);
            else
                iMonth = objCalendarDate2.get(Calendar.MONTH) - objCalendarDate1.get(Calendar.MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iMonth;
    }
}

Related

  1. getMonthOfThisYear(Date currentdate)
  2. getMonthOfYear(Date date)
  3. getMonthOfYear(Date dt)
  4. getMonthOfYear(final Date date)
  5. getMonthRate(Date date, boolean moveIn)
  6. getMonthStart(Date date)
  7. getMonthStart(Date date)
  8. getMonthStartDay(Date date)
  9. getNextMonth(Date baseDate, int Month)