Java Day of Week truncDateToIsoWeek(Date d)

Here you can find the source of truncDateToIsoWeek(Date d)

Description

truncate the date to week (does not depend on locale, monday is always the first day in a week)

License

Open Source License

Declaration

public static Date truncDateToIsoWeek(Date d) 

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.java  2 s  . c  o m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

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

public class Main {
    /**
     * truncate the date to week (does not depend on locale, monday is always the first day in a week)
     *
     * @see ISO 8601
     */
    public static Date truncDateToIsoWeek(Date d) {
        if (d == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        truncCalendarToIsoWeek(c, -1);
        return c.getTime();
    }

    /**
     * Truncates to monday, see ISO 8601
     *
     * @param c
     *          Calendar to truncate
     * @param adjustIncrement
     *          -1 to back in time, +1 to go forward
     */
    public static void truncCalendarToIsoWeek(Calendar c, int adjustIncrement) {
        truncCalendarToWeek(c, adjustIncrement, Calendar.MONDAY);
    }

    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. previousWeek(long date)
  2. previousWeek(long date)
  3. startOfWeek(Date date)
  4. toStartWeek(Date date)
  5. toWeekDayUS(Date date, int expect)
  6. weekCount(Date start, Date end)
  7. weekDay(final Date date)
  8. weekDaysBetween(final Date a, final Date b)
  9. WeekOfTheYear(Date dt)