Java Day Between monthsDiff(final Date data1, final Date data2)

Here you can find the source of monthsDiff(final Date data1, final Date data2)

Description

Get the difference in months of two given dates.

License

Open Source License

Parameter

Parameter Description
data1 The first date
data2 The second date

Return

The difference in months of two given dates

Declaration

public static int monthsDiff(final Date data1, final Date data2) 

Method Source Code


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

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

public class Main {
    /**//from  w w  w . j a v a 2 s  .com
     * Get the difference in months of two given dates.
     * 
     * @param data1
     *            The first date
     * @param data2
     *            The second date
     * @return The difference in months of two given dates
     */
    public static int monthsDiff(final Date data1, final Date data2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(data1);
        int monthA = cal.get(Calendar.MONTH);
        int yearA = cal.get(Calendar.YEAR);
        cal.setTime(data2);
        int monthB = cal.get(Calendar.MONTH);
        int yearB = cal.get(Calendar.YEAR);
        return ((yearA - yearB) * cal.getMaximum(Calendar.MONTH)) + (monthA - monthB);
    }
}

Related

  1. internalTrimOrAlterDate(Date date, boolean trim, int dayDiff)
  2. monthDiff(Date beginDate, Date endDate)
  3. monthDiff(Date from, Date to)
  4. monthDiff(Date fromDate, Date toDate)
  5. monthDifference(final Date a, final Date b)
  6. stdNewDate(String tzDiff)
  7. substractDate(final Date date, final Integer different)
  8. timeDifferenceInSeconds(Date startDate, Date endDate)
  9. timeDifferenceWithCurrentTime(Date startTime)