Java Day Between monthDifference(final Date a, final Date b)

Here you can find the source of monthDifference(final Date a, final Date b)

Description

month Difference

License

Open Source License

Declaration

public static int monthDifference(final Date a, final Date b) 

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 monthDifference(final Date a, final Date b) {
        final Calendar calendarA = Calendar.getInstance();
        final Calendar calendarB = Calendar.getInstance();
        int multiplier;
        if (b.getTime() - a.getTime() > 0) {
            multiplier = -1;// w  ww.  j  a  v  a2  s  .  c o  m
            calendarA.setTime(b);
            calendarB.setTime(a);
        } else {
            multiplier = 1;
            calendarA.setTime(a);
            calendarB.setTime(b);
        }

        int result = 0;
        final int years = calendarA.get(Calendar.YEAR) - calendarB.get(Calendar.YEAR);
        final int months = calendarA.get(Calendar.MONTH) - calendarB.get(Calendar.MONTH);
        final int days = calendarA.get(Calendar.DAY_OF_MONTH) - calendarB.get(Calendar.DAY_OF_MONTH);

        result += years * 12;
        result += months;

        if (days < 0) {
            result -= 1;
        }
        return result * multiplier;
    }
}

Related

  1. hourDiff(Date firstDate, Date lastDate)
  2. internalTrimOrAlterDate(Date date, boolean trim, int dayDiff)
  3. monthDiff(Date beginDate, Date endDate)
  4. monthDiff(Date from, Date to)
  5. monthDiff(Date fromDate, Date toDate)
  6. monthsDiff(final Date data1, final Date data2)
  7. stdNewDate(String tzDiff)
  8. substractDate(final Date date, final Integer different)
  9. timeDifferenceInSeconds(Date startDate, Date endDate)