Java Calendar Week endOfWeek(Calendar calendar, Date date)

Here you can find the source of endOfWeek(Calendar calendar, Date date)

Description

Adjusts the calendar to the end of the current week.

License

Open Source License

Parameter

Parameter Description
calendar calendar to adjust.
date the Date to use.

Return

the end of the week of the given date

Declaration

public static Date endOfWeek(Calendar calendar, Date date) 

Method Source Code

//package com.java2s;
/*/*from  w w w . java2 s  . c om*/
 * #%L
 * The AIBench Plugin Manager Plugin
 * %%
 * Copyright (C) 2006 - 2016 Daniel Glez-Pe?a and Florentino Fdez-Riverola
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * Adjusts the calendar to the end of the current week.
     * That is, last day of the week with all time fields at max.
     * @param calendar the calendar to adjust.
     */
    public static void endOfWeek(Calendar calendar) {
        startOfWeek(calendar);
        calendar.add(Calendar.DATE, 7);
        calendar.add(Calendar.MILLISECOND, -1);
    }

    /**
     * Adjusts the calendar to the end of the current week.
     * That is, last day of the week with all time fields at max.
     * The Date of the adjusted Calendar is
     * returned. 
     * 
     * @param calendar calendar to adjust.
     * @param date the Date to use.
     * @return the end of the week of the given date
     */
    public static Date endOfWeek(Calendar calendar, Date date) {
        calendar.setTime(date);
        endOfWeek(calendar);
        return calendar.getTime();
    }

    /**
     * Adjusts the calendar to the start of the current week.
     * That is, first day of the week with all time fields cleared.
     * @param calendar the calendar to adjust.
     */
    public static void startOfWeek(Calendar calendar) {
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        startOfDay(calendar);
    }

    /**
     * Adjusts the calendar to the start of the current week.
     * That is, last day of the week with all time fields at max.
     * The Date of the adjusted Calendar is
     * returned. 
     * 
     * @param calendar calendar to adjust.
     * @param date the Date to use.
     * @return the start of the week of the given date
     */
    public static Date startOfWeek(Calendar calendar, Date date) {
        calendar.setTime(date);
        startOfWeek(calendar);
        return calendar.getTime();
    }

    /**
     * Adjust the given calendar to the first millisecond of the given date.
     * that is all time fields cleared. The Date of the adjusted Calendar is
     * returned. 
     * 
     * @param calendar calendar to adjust.
     * @param date the Date to use.
     * @return the start of the day of the given date
     */
    public static Date startOfDay(Calendar calendar, Date date) {
        calendar.setTime(date);
        startOfDay(calendar);
        return calendar.getTime();
    }

    /**
     * Adjust the given calendar to the first millisecond of the current day.
     * that is all time fields cleared.
     * 
     * @param calendar calendar to adjust.
     */
    public static void startOfDay(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
    }
}

Related

  1. convertIntToCalendarDayOfWeek(int dayOfWeek)
  2. firstDayOfLastWeek(Calendar c)
  3. getADayOfWeek(Calendar day, int dayOfWeek)
  4. getDateOfLastDayOfWeekInMonth(Calendar calendar, int dayOfWeek)
  5. getDayFromPreviousWeek(Calendar requestDate)