Java Date Before getDistanceMonthOfTwoDate(Date before, Date after)

Here you can find the source of getDistanceMonthOfTwoDate(Date before, Date after)

Description

get Distance Month Of Two Date

License

Open Source License

Declaration

public static int getDistanceMonthOfTwoDate(Date before, Date after) 

Method Source Code


//package com.java2s;

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

public class Main {

    public static int getDistanceMonthOfTwoDate(Date before, Date after) {
        if (before.compareTo(after) >= 0) {
            return 0;
        }//from  w  ww.j  av  a  2 s  .  co  m
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTime(before);
        calendar2.setTime(after);
        int distance = (calendar2.get(Calendar.YEAR) - calendar1.get(Calendar.YEAR)) * 12
                + calendar2.get(Calendar.MONTH) - calendar1.get(Calendar.MONTH);
        if (calendar2.get(Calendar.DAY_OF_MONTH) < calendar1.get(Calendar.DAY_OF_MONTH)) {
            distance--;
        }
        return distance;
    }
}

Related

  1. getDateBeforeToday(int num)
  2. getDateBeforTwelveMonth()
  3. getDateTimeAfterOfBeforeHour(Integer AfterOfBeforeHour)
  4. getDayBeforeOrAfter2(Date time, int days)
  5. getDayFormSomeDay(Date date, int n, boolean before)
  6. getMinuteBefore(Date date, int minute)
  7. getMonth(Date time, int monthBefore, int monthAfter)
  8. getMonthBefore(Date currentDate, int numberMonthBefore)
  9. getMonthStartDateBeforeCurrent(int monthNum, Date current)