Java Calendar Different countDiffMonth(Calendar cBegin, Calendar cEnd, boolean isRoundUp)

Here you can find the source of countDiffMonth(Calendar cBegin, Calendar cEnd, boolean isRoundUp)

Description

count Diff Month

License

Open Source License

Declaration

public static int countDiffMonth(Calendar cBegin, Calendar cEnd, boolean isRoundUp) 

Method Source Code

//package com.java2s;
/*// w w  w.j a va2  s .  c o m
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Calendar;

public class Main {
    public static int countDiffMonth(Calendar cBegin, Calendar cEnd, boolean isRoundUp) {
        int diffMonth = 0;
        int dBegin = 0;
        int dEnd = 0;
        int mBegin = 0;
        int mEnd = 0;
        try {
            dBegin = cBegin.get(Calendar.DATE);
            dEnd = cEnd.get(Calendar.DATE);
            mBegin = (cBegin.get(Calendar.YEAR) * 12) + cBegin.get(Calendar.MONTH);
            mEnd = (cEnd.get(Calendar.YEAR) * 12) + cEnd.get(Calendar.MONTH);

            diffMonth = mEnd - mBegin;
            if (!isRoundUp) {
                return diffMonth;
            }
            if (dEnd > dBegin) {
                if (diffMonth >= 0) {
                    diffMonth++;
                } else {
                    diffMonth--;
                }
            }
            return diffMonth;
        } finally {

        }
    }
}

Related

  1. countDiffDay(Calendar c1, Calendar c2)
  2. datediff(Calendar c1, Calendar c2)
  3. dateDiff(int type, Calendar fromDate, Calendar toDate)
  4. dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
  5. dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)