returns true if there are missing months between first and second points Note: The first point is before second point - Java java.util

Java examples for java.util:Month

Description

returns true if there are missing months between first and second points Note: The first point is before second point

Demo Code

/*//from  w w w.j  av 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 firstPoint = Calendar.getInstance();
        Calendar secondPoint = Calendar.getInstance();
        System.out.println(areMonthsMissing(firstPoint, secondPoint));
    }

    /**
     * returns true if there are missing months between first and second points
     *
     * <br>
     * Note: The first point is before second point
     */
    public static boolean areMonthsMissing(Calendar firstPoint,
            Calendar secondPoint) {

        boolean missing = true;

        int yearOfFirstPoint = getYear(firstPoint);
        int yearOfCurrentPoint = getYear(secondPoint);
        int monthOfFirstPoint = getMonth(firstPoint);
        int monthOfSecondPoint = getMonth(secondPoint);

        // if both dates on the same month
        if (isSameMonthOfYear(firstPoint, secondPoint))
            missing = false;
        // else if same year check if consecutive months of the year
        if ((yearOfFirstPoint == yearOfCurrentPoint)
                && (monthOfFirstPoint + 1 == monthOfSecondPoint)) {
            missing = false;
        }
        // if different years check if consecutive months of the two years
        else if (yearOfFirstPoint + 1 == yearOfCurrentPoint) {

            Calendar tempCalendar = (Calendar) firstPoint.clone();
            incrementOneMonth(tempCalendar);
            if (isSameMonthOfYear(tempCalendar, secondPoint))
                missing = false;
        }

        return missing;
    }

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

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

    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 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