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

Java examples for java.util:Week

Description

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

Demo Code

/*//w w  w. j  a v a2  s.  com
 * 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(areWeeksMissing(firstPoint, secondPoint));
    }

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

        boolean missing = true;

        int yearOfFirstPoint = getYear(firstPoint);
        int yearOfCurrentPoint = getYear(secondPoint);
        int weekOfFirstPoint = getWeek(firstPoint);
        int weekOfSecondPoint = getWeek(secondPoint);

        // if both dates on the same week
        if (isSameWeekOfYear(firstPoint, secondPoint))
            missing = false;
        // else if same year check if consecutive weeks of the year
        else if ((yearOfFirstPoint == yearOfCurrentPoint)
                && (weekOfFirstPoint + 1 == weekOfSecondPoint)) {
            missing = false;
        }
        // if different years check if consecutive weeks of the two years
        else if (yearOfFirstPoint + 1 == yearOfCurrentPoint) {

            Calendar tempCalendar = (Calendar) firstPoint.clone();
            incrementOneWeek(tempCalendar);
            if (isSameWeekOfYear(tempCalendar, secondPoint))
                missing = false;
        }

        return missing;
    }

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

    public static int getWeek(Calendar calendar) {
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

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

        boolean sameWeek = false;

        // if same year check if same week
        if (getYear(calendar1) == getYear(calendar2)) {
            if (getWeek(calendar1) == getWeek(calendar2))
                sameWeek = true;
        }
        /*
         * else if different years check if the two points fall in the same
         * week. e.g : 29th December 2003 and 2nd January 2004 fall in different
         * years but same week
         */
        else {
            Calendar calendar1Temp = (Calendar) calendar1.clone();
            Calendar calendar2Temp = (Calendar) calendar2.clone();
            setTimeAsFirstDayOfWeek(calendar1Temp);
            setTimeAsFirstDayOfWeek(calendar2Temp);
            if (isSameDayOfYear(calendar1Temp, calendar2Temp))
                sameWeek = true;
        }

        return sameWeek;
    }

    public static void incrementOneWeek(Calendar calendar) {

        int monthBeforeIncrement = calendar.get(Calendar.MONTH);
        calendar.add(Calendar.WEEK_OF_YEAR, 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 setTimeAsFirstDayOfWeek(Calendar calendar) {

        int monthBeforeSet = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        int monthAfterSet = calendar.get(Calendar.MONTH);

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

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

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

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

    public static void normalise(Calendar calendar) {

        Calendar tempCalendar = (Calendar) calendar.clone();
        calendar.clear();
        calendar.set(Calendar.YEAR, tempCalendar.get(Calendar.YEAR));
        calendar.set(Calendar.DAY_OF_YEAR,
                tempCalendar.get(Calendar.DAY_OF_YEAR));
    }

    public static int getDay(Calendar calendar) {
        return calendar.get(Calendar.DAY_OF_YEAR);
    }
}

Related Tutorials