Java Day of Week endOfWeek(Date inDate, TimeZone timeZone)

Here you can find the source of endOfWeek(Date inDate, TimeZone timeZone)

Description

Calculate the start of the week for the given date.

License

Open Source License

Parameter

Parameter Description
inDate The given date.

Return

The date at the start of the month.

Declaration

public static Calendar endOfWeek(Date inDate, TimeZone timeZone) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//  www  .  ja v a  2  s. co m
     * Calculate the start of the week for the given date.
     * 
     * @param inDate The given date.
     * 
     * @return The date at the start of the month.
     */
    public static Calendar endOfWeek(Date inDate, TimeZone timeZone) {
        Calendar c1 = Calendar.getInstance(timeZone);
        c1.setTime(inDate);

        Calendar c2 = Calendar.getInstance(timeZone);
        c2.clear();

        int daysToAdd = 8 - isoDayOfWeek(c1.get(Calendar.DAY_OF_WEEK));

        c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH) + daysToAdd);

        return c2;
    }

    private static int isoDayOfWeek(int javaDayOfWeek) {

        switch (javaDayOfWeek) {
        case Calendar.MONDAY:
            return 1;
        case Calendar.TUESDAY:
            return 2;
        case Calendar.WEDNESDAY:
            return 3;
        case Calendar.THURSDAY:
            return 4;
        case Calendar.FRIDAY:
            return 5;
        case Calendar.SATURDAY:
            return 6;
        case Calendar.SUNDAY:
            return 7;
        default:
            throw new IllegalArgumentException("Invalid day of week " + javaDayOfWeek);
        }
    }
}

Related

  1. dayOfWeek(int dayMark)
  2. dayOfWeek(int year, int month, int day)
  3. dayOfWeekFromInt(final int theDay)
  4. dayOfWeekFromInteger(String aDay, boolean longName)
  5. dayOfWeekNames()
  6. extractDayOfWeek(Date date)
  7. findDayOfWeek(String threeLetters)
  8. firstDateAfterAddWeeks(Date early, int weeks)
  9. firstDayOfWeek()