Java Date Difference differenceMonth(Date date1, Date date2)

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

Description

difference Month

License

Open Source License

Declaration

public static int differenceMonth(Date date1, Date date2) 

Method Source Code


//package com.java2s;

import java.util.Calendar;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    private static TimeZone timeZone = TimeZone.getTimeZone("Asia/Tokyo");

    public static int differenceMonth(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance(timeZone);
        cal1.setTime(date1);/*w  ww.j  a v a2s  .  co  m*/
        cal1.set(Calendar.DATE, 1);
        Calendar cal2 = Calendar.getInstance(timeZone);
        cal2.setTime(date2);
        cal2.set(Calendar.DATE, 1);
        int count = 0;
        if (cal1.before(cal2)) {
            while (cal1.before(cal2)) {
                cal1.add(Calendar.MONTH, 1);
                count--;
            }
        } else {
            count--;
            while (!cal1.before(cal2)) {
                cal1.add(Calendar.MONTH, -1);
                count++;
            }
        }
        return count;
    }
}

Related

  1. difference(Date date1, Date date2)
  2. differenceDay(Date toDate, Date fromDate)
  3. DifferenceInMilliseconds(Date date1, Date date2)
  4. differenceInMonths(Date beginningDate, Date endingDate)
  5. DifferenceInSeconds(Date date1, Date date2)
  6. differenceOnMonth(Date date1, Date date2)
  7. differenceOnYear(Date date1, Date date2)
  8. diffIntoMilliSecond(java.util.Date date, java.util.Date date1)
  9. diffMillis(Date d1, Date d2)