Java Calendar Week getWeekStart(boolean startSunday, Calendar date)

Here you can find the source of getWeekStart(boolean startSunday, Calendar date)

Description

Returns a date corresponding to the first day of the week.

License

Open Source License

Parameter

Parameter Description
startSunday Whether the week starts on Sunday.
date A date to calculate the start for.

Return

Calendar representation of the first day of the week.

Declaration

public static Calendar getWeekStart(boolean startSunday, Calendar date) 

Method Source Code


//package com.java2s;
/* //from  w w  w  . j  a v a  2  s. c om
 * Copyright (C) 2014 Alex Pavlunenko <alexp at xpresstek.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Returns a date corresponding to the first day of the week.
     *
     * @param startSunday Whether the week starts on Sunday.
     * @param date A date to calculate the start for.
     * @return Calendar representation of the first day of the week.
     */
    public static Calendar getWeekStart(boolean startSunday, Calendar date) {

        Calendar start = new GregorianCalendar();
        if (date != null) {
            start.setTime(date.getTime());
        }
        int current_dow = start.get(Calendar.DAY_OF_WEEK);
        start.add(Calendar.DAY_OF_MONTH, -current_dow);
        start.add(Calendar.DAY_OF_MONTH, startSunday ? Calendar.SUNDAY : Calendar.MONDAY);
        start.setTime(getDayStart(start.getTime()));
        return start;
    }

    /**
     * Calculates start of the day
     *
     * @param date Date to use in calculation
     * @return Date with hour min and sec set to 0
     */
    public static Date getDayStart(Date date) {
        Calendar start = new GregorianCalendar();
        start.setTime(date);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        return start.getTime();
    }
}

Related

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