Java Calendar Adjust truncCalendarToWeek(Calendar c, int adjustIncrement)

Here you can find the source of truncCalendarToWeek(Calendar c, int adjustIncrement)

Description

trunc Calendar To Week

License

Open Source License

Declaration

public static void truncCalendarToWeek(Calendar c, int adjustIncrement) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * 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 a  v  a2s  . co  m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.util.Calendar;

public class Main {
    public static void truncCalendarToWeek(Calendar c, int adjustIncrement) {
        truncCalendarToWeek(c, adjustIncrement, Calendar.getInstance().getFirstDayOfWeek());
    }

    /**
     * truncate the calendar to week
     *
     * @param adjustIncrement
     *          +1 or -1
     */
    public static void truncCalendarToWeek(Calendar c, int adjustIncrement, int firstDayOfWeek) {
        if (c == null) {
            return;
        }
        if (adjustIncrement < -1) {
            adjustIncrement = -1;
        }
        if (adjustIncrement > 1) {
            adjustIncrement = 1;
        }
        if (adjustIncrement == 0) {
            adjustIncrement = -1;
        }
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        while (c.get(Calendar.DAY_OF_WEEK) != firstDayOfWeek) {
            c.add(Calendar.DATE, adjustIncrement);
        }
    }
}

Related

  1. adjustToDayEnd(Calendar cal)
  2. adjustToFieldStart(Calendar cal, int[] fields)
  3. adjustToMonthStart(Calendar cal)
  4. getMidnightCalendarByUnadjustedDate(Date date, TimeZone timezone)
  5. truncCalendarToWeek(Calendar c, int adjustIncrement)