Android Week Compare isSameWeek(Time t1, Time t2)

Here you can find the source of isSameWeek(Time t1, Time t2)

Description

is Same Week

License

Apache License

Declaration

public static final boolean isSameWeek(Time t1, Time t2) 

Method Source Code

//package com.java2s;
/*//from   w  ww. j  a  v a2s .c om
 * Copyright (C) 2011 The original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import android.text.format.Time;

public class Main {
    public static final boolean isSameWeek(Time t1, Time t2) {
        // A quick calculation for the most frequent case.
        if (isSameDate(t1, t2)) {
            return true;
        }

        // Sort the two dates. x.date <= y.date
        final Time x;
        final Time y;

        if (t1.before(t2)) {
            x = t1;
            y = t2;
        } else {
            x = t2;
            y = t1;
        }

        // Get the beginning of day y.
        final Time t = new Time();
        t.set(y.monthDay, y.month, y.year);

        y.normalize(false);
        int n = y.weekDay;
        // Substract n days
        while (n > 0) {
            final int daysLeftInMonth = t.monthDay - 1;
            // Go across month/year boundary
            if (daysLeftInMonth == 0) {
                decrementOneDay(t);
                n--;
                continue;
            }

            // Decrement within the month
            final int d = Math.min(n, daysLeftInMonth);
            t.monthDay -= d;
            n -= d;
        }

        return !isEarilerDate(x, t);
    }

    public static final boolean isSameDate(Time t1, Time t2) {
        return (t1.monthDay == t2.monthDay) && (t1.month == t2.month)
                && (t1.year == t2.year);
    }

    private static final void decrementOneDay(Time t) {
        if (t.monthDay > 1) {
            t.monthDay--;
            return;
        }

        if (t.month > 0) {
            t.month--;

        } else {
            t.year--;
            t.month = 11;
        }

        // Set day to end of month
        t.monthDay = 1; // temporary, have a valid date
        t.monthDay = t.getActualMaximum(Time.MONTH_DAY);
    }

    public static final boolean isEarilerDate(Time t1, Time t2) {
        if (t1.year != t2.year) {
            return t1.year < t2.year;
        }
        if (t1.month != t2.month) {
            return t1.month < t2.month;
        }
        return t1.monthDay < t2.monthDay;
    }
}

Related

  1. isWithinTheWeek(Date sunday, Date now)
  2. isSameWeek(Date date)