Java Calendar Week isThisWeek(Calendar time)

Here you can find the source of isThisWeek(Calendar time)

Description

is This Week

License

Open Source License

Declaration

public static boolean isThisWeek(Calendar time) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2009 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w w  w.  j av a2  s  .c  o m
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

import java.util.Calendar;

public class Main {
    private static int startDay = Calendar.MONDAY;

    public static boolean isThisWeek(Calendar time) {
        if (time != null) {
            Calendar weekStart = getCalendar();
            snapStartOfWorkWeek(weekStart);
            Calendar weekEnd = getCalendar();
            snapEndOfWeek(weekEnd);
            return (time.compareTo(weekStart) >= 0 && time.compareTo(weekEnd) <= 0);
        }
        return false;
    }

    public static Calendar getCalendar() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(startDay);
        cal.getTime();
        return cal;
    }

    public static Calendar snapStartOfWorkWeek(Calendar cal) {
        cal.set(Calendar.DAY_OF_WEEK, startDay);
        snapStartOfDay(cal);
        return cal;
    }

    public static Calendar snapEndOfWeek(Calendar cal) {

        cal.set(Calendar.DAY_OF_WEEK, getLastDayOfWeek(cal));

        snapEndOfDay(cal);
        return cal;
    }

    public static Calendar snapStartOfDay(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.getTime();
        return cal;
    }

    public static int getLastDayOfWeek(Calendar cal) {
        int last = cal.getFirstDayOfWeek() - 1;

        if (last == 0) {
            last = Calendar.SATURDAY;
        }

        return last;
    }

    public static Calendar snapEndOfDay(Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
        cal.getTime();
        return cal;
    }
}

Related

  1. getWeekDay(Calendar cal)
  2. getWeekdayPosition(Calendar jCalendar)
  3. getWeekEnd(boolean startSunday, Calendar date)
  4. getWeekOfCurrentDayWithoutStartShift(Calendar cal)
  5. getWeekStart(boolean startSunday, Calendar date)
  6. isWeekday(Calendar cal)
  7. isWeekEnd(Calendar c)
  8. isWeekend(Calendar cal)
  9. isWeekend(GregorianCalendar date)