get Week Date Range - Java java.util

Java examples for java.util:Week

Description

get Week Date Range

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Main {

    public static List<Date> getWeekDateRange(Date date, int weekSeq) {
        List<Date> dateRange = new ArrayList<Date>();
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(date);//from   w ww.ja va 2 s  . c om
        startDate.add(Calendar.WEEK_OF_YEAR, weekSeq);
        Date newDate = startDate.getTime();
        dateRange.add(getMondayDate(newDate));
        dateRange.add(getSundayDate(newDate));
        return dateRange;
    }

    public static Date getMondayDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        day = (day + 6) % 7;
        day = day == 0 ? 7 : day;
        calendar.add(Calendar.DAY_OF_YEAR, 1 - day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getSundayDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        day = (day + 6) % 7;
        day = day == 0 ? 7 : day;
        calendar.add(Calendar.DAY_OF_YEAR, 7 - day);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
}

Related Tutorials