Java Day of Week getLastDayOfWeek(Date date)

Here you can find the source of getLastDayOfWeek(Date date)

Description

get Last Day Of Week

License

Open Source License

Declaration

public static Date getLastDayOfWeek(Date date) 

Method Source Code

//package com.java2s;

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

public class Main {

    public static Date getLastDayOfWeek(int year, int week) {
        week = week - 1;// www.  j a va 2 s  . c  o m
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);
        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);

        return getLastDayOfWeek(cal.getTime());
    }

    public static Date getLastDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
        return calendar.getTime();
    }

    public static Date getFirstDayOfWeek(int year, int week) {
        week = week - 1;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);

        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }

    public static Date getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); // Sunday
        return calendar.getTime();
    }
}

Related

  1. getIsoDayOfWeek(Date date)
  2. getIsoWeek(java.util.Date date)
  3. getLastDayFromLastWeek(Date d)
  4. getLastDayOfTheWeek(Date date)
  5. getLastDayOfWeek(Date date)
  6. getLastDayOfWeek(Date date)
  7. getLastDayOfWeek(Date date)
  8. getLastDayOfWeek(Date date)
  9. getLastDayOfWeek(String str, int week)