Java Month Difference getDiffMonth(Date start, Date end)

Here you can find the source of getDiffMonth(Date start, Date end)

Description

get Diff Month

License

Apache License

Declaration

public static int getDiffMonth(Date start, Date end) 

Method Source Code

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

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

public class Main {

    public static int getDiffMonth(Date start, Date end) {
        //        Calendar startCalendar = Calendar.getInstance();
        //        startCalendar.setTime(start);
        //        Calendar endCalendar = Calendar.getInstance();
        //        endCalendar.setTime(end);
        //        Calendar temp = Calendar.getInstance();
        //        temp.setTime(end);
        //        temp.add(Calendar.DATE, 1);
        //        int year = endCalendar.get(Calendar.YEAR)
        //                - startCalendar.get(Calendar.YEAR);
        //        int month = endCalendar.get(Calendar.MONTH)
        //                - startCalendar.get(Calendar.MONTH);
        //        if ((startCalendar.get(Calendar.DATE) == 1)
        //                && (temp.get(Calendar.DATE) == 1)) {
        //            return year * 12 + month + 1;
        //        } else if ((startCalendar.get(Calendar.DATE) != 1)
        //                && (temp.get(Calendar.DATE) == 1)) {
        //            return year * 12 + month + 1;
        //        } else if ((startCalendar.get(Calendar.DATE) == 1)
        //                && (temp.get(Calendar.DATE) != 1)) {
        //            return year * 12 + month + 1;
        //        } else {
        //            return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
        //        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);//  www . j a va  2  s  .c  o  m
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        return (endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR)) * 12
                + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
    }
}

Related

  1. getDiffBetweenMonth(Date latestDate, Date current)
  2. getDiffMonth(Date begin, Date end)
  3. getDiffMonths(Date date1, Date date2)