Java Day Between monthDiff(Date beginDate, Date endDate)

Here you can find the source of monthDiff(Date beginDate, Date endDate)

Description

month Diff

License

Apache License

Declaration

public static int monthDiff(Date beginDate, Date endDate) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final int ONE_YEAR_MONTH_TOTAL = 12;

    public static int monthDiff(Date beginDate, Date endDate) {
        if (beginDate == null || endDate == null) {
            return 0;
        }//from w  ww. j  ava2 s .c  o m
        Calendar beginCal = Calendar.getInstance();
        beginCal.setTime(beginDate);

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);

        int yearDiff = endCal.get(Calendar.YEAR) - beginCal.get(Calendar.YEAR);
        int monthDiff = ONE_YEAR_MONTH_TOTAL * yearDiff + endCal.get(Calendar.MONTH) - beginCal.get(Calendar.MONTH);

        return monthDiff;
    }
}

Related

  1. getWorkingDaysBetween(Date fromDate, Date toDate)
  2. getYearDiff(Date date1, Date date2)
  3. getYearsDifference(Date startTime, Date endTime)
  4. hourDiff(Date firstDate, Date lastDate)
  5. internalTrimOrAlterDate(Date date, boolean trim, int dayDiff)
  6. monthDiff(Date from, Date to)
  7. monthDiff(Date fromDate, Date toDate)
  8. monthDifference(final Date a, final Date b)
  9. monthsDiff(final Date data1, final Date data2)