returns number of months from date of startPoint till date of endPoint 1. - Java java.util

Java examples for java.util:Month

Description

returns number of months from date of startPoint till date of endPoint 1.

Demo Code

/*//ww  w  . ja  v a2  s.  c o m
 * Copyright 2009 Yodlee, Inc.  All Rights Reserved.  Your use of this code 
 * requires a license from Yodlee.  Any such license to this code is 
 * restricted to evaluation/illustrative purposes only. It is not intended 
 * for use in a production environment, and Yodlee disclaims all warranties 
 * and/or support obligations concerning this code, regardless of the terms 
 * of any other agreements between Yodlee and you."
 */
//package com.java2s;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar startPoint = Calendar.getInstance();
        Calendar endPoint = Calendar.getInstance();
        System.out.println(calculateMonthsInBetween(startPoint, endPoint));
    }

    /**
     * returns number of months from date of startPoint till date of endPoint
     *
     * <pre>
     *     1. returns 0 if startPoint and endPoint are in the same month
     *     2. returns a positive no. if month of startPoint is before month of endPoint
     *     3. returns a negative no. if month of startPoint is after month of endPoint
     * </pre>
     *
     * @param startPoint
     * @param endPoint
     *
     * @return returns number of months from date of startPoint till date of
     *         endPoint
     */
    public static int calculateMonthsInBetween(Calendar startPoint,
            Calendar endPoint) {

        int numMonths = 0;

        Calendar tempCalendar = (Calendar) startPoint.clone();
        int compare = startPoint.getTime().compareTo(endPoint.getTime());
        if (compare <= 0) {

            while (!isSameMonthOfYear(tempCalendar, endPoint)) {
                incrementOneMonth(tempCalendar);
                numMonths++;
            }
        } else {

            while (!isSameMonthOfYear(tempCalendar, endPoint)) {
                decrementOneMonth(tempCalendar);
                numMonths--;
            }
        }

        return numMonths;
    }

    public static boolean isSameMonthOfYear(Calendar calendar1,
            Calendar calendar2) {

        return (getYear(calendar1) == getYear(calendar2))
                && (getMonth(calendar1) == getMonth(calendar2));
    }

    public static void incrementOneMonth(Calendar calendar) {

        int monthBeforeIncrement = calendar.get(Calendar.MONTH);
        calendar.add(Calendar.MONTH, 1);
        int monthAfterIncrement = calendar.get(Calendar.MONTH);

        // if year changes setMinimalDaysInFirstWeek for New Year
        if (monthBeforeIncrement == Calendar.DECEMBER
                && monthAfterIncrement == Calendar.JANUARY) {
            calendar.setMinimalDaysInFirstWeek(calculateMinimalDaysInFirstWeek(calendar));
        }
    }

    public static void decrementOneMonth(Calendar calendar) {

        int monthBeforeDecrement = calendar.get(Calendar.MONTH);
        calendar.add(Calendar.MONTH, -1);
        int monthAfterDecrement = calendar.get(Calendar.MONTH);

        // if year changes setMinimalDaysInFirstWeek for New Year
        if (monthBeforeDecrement == Calendar.JANUARY
                && monthAfterDecrement == Calendar.DECEMBER) {
            calendar.setMinimalDaysInFirstWeek(calculateMinimalDaysInFirstWeek(calendar));
        }
    }

    public static int getYear(Calendar calendar) {
        return calendar.get(Calendar.YEAR);
    }

    public static int getMonth(Calendar calendar) {
        return calendar.get(Calendar.MONTH);
    }

    public static int calculateMinimalDaysInFirstWeek(Calendar calendar) {

        Calendar temp = (Calendar) calendar.clone();
        temp.set(Calendar.DAY_OF_YEAR, 1);
        int firstDayOfJan = temp.get(Calendar.DAY_OF_WEEK);
        return 8 - firstDayOfJan;
    }
}

Related Tutorials