WeekdayUtil

/**
 * 获取星期六和星期日工具类
 */
public class WeekdayUtil {

    /**
     * 获取当前年所有星期六
     * @param year 年份
     * @return 日期集合
     */
    public static List getSaturdayInYear(int year){
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        int i = 1;
        while (calendar.get(Calendar.YEAR) < year + 1) {
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                //dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
        }
        return dateList;
    }

    /**
     * 获取当前年所有星期日
     * @param year 年份
     * @return 日期集合
     */
    public static List getSundayInYear(int year){
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        int i = 1;
        while (calendar.get(Calendar.YEAR) < year + 1) {
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                //dateList.add(dateFormat.format(calendar.getTime()));
            }
        }
        return dateList;
    }

    /**
     * 获取当前年所有星期六和星期日
     * @param year 年份
     * @return 日期集合
     */
    public static List getWeekendInYear(int year){
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        int i = 1;
        while (calendar.get(Calendar.YEAR) < year + 1) {
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
        }
        return dateList;
    }

    /**
     * 获取当前年某天到年底所有星期六
     * @param year 当前年
     * @param yyyy_MM_dd 某天
     * @return 日期集合
     */
    public static List getSaturdayInYearAfterDay(int year,String yyyy_MM_dd){
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        int i = 1;
        while (calendar.get(Calendar.YEAR) < year + 1) {
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year && Integer.parseInt(dateFormat.format(calendar.getTime()).replaceAll("-","")) >= Integer.parseInt(yyyy_MM_dd.replaceAll("-",""))) {
                //dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year && Integer.parseInt(dateFormat.format(calendar.getTime()).replaceAll("-","")) >= Integer.parseInt(yyyy_MM_dd.replaceAll("-",""))) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
        }
        return dateList;
    }

    /**
     * 获取当前年某天到年底所有星期日
     * @param year 当前年
     * @param yyyy_MM_dd 某天
     * @return 日期集合
     */
    public static List getSundayInYearAfterDay(int year,String yyyy_MM_dd){
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        int i = 1;
        while (calendar.get(Calendar.YEAR) < year + 1) {
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year && Integer.parseInt(dateFormat.format(calendar.getTime()).replaceAll("-","")) >= Integer.parseInt(yyyy_MM_dd.replaceAll("-",""))) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year && Integer.parseInt(dateFormat.format(calendar.getTime()).replaceAll("-","")) >= Integer.parseInt(yyyy_MM_dd.replaceAll("-",""))) {
                //dateList.add(dateFormat.format(calendar.getTime()));
            }
        }
        return dateList;
    }

    /**
     * 获取当前年某天到年底所有星期六和星期日
     * @param year 当前年
     * @param yyyy_MM_dd 某天
     * @return 日期集合
     */
    public static List getWeekendInYearAfterDay(int year,String yyyy_MM_dd){
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        int i = 1;
        while (calendar.get(Calendar.YEAR) < year + 1) {
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year && Integer.parseInt(dateFormat.format(calendar.getTime()).replaceAll("-","")) >= Integer.parseInt(yyyy_MM_dd.replaceAll("-",""))) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year && Integer.parseInt(dateFormat.format(calendar.getTime()).replaceAll("-","")) >= Integer.parseInt(yyyy_MM_dd.replaceAll("-",""))) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
        }
        return dateList;
    }

    /**
     * 获取当月的所有星期六
     * @param year 年份
     * @param month 月份
     * @return 日期集合
     */
    public static List getSaturdayInMonth(int year, int month) {
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List list = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < daySize; i++) {
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            if (week == Calendar.SATURDAY) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
        }
        return dateList;
    }

    /**
     * 获取当月的所有星期日
     * @param year 年份
     * @param month 月份
     * @return 日期集合
     */
    public static List getSundayInMonth(int year, int month) {
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List list = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < daySize; i++) {
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            if (week == Calendar.SUNDAY) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
        }
        return dateList;
    }

    /**
     * 获取当月的所有星期六和星期日
     * @param year 年份
     * @param month 月份
     * @return 日期集合
     */
    public static List getWeekendInMonth(int year, int month) {
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List list = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < daySize; i++) {
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            if (week == Calendar.SATURDAY ) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            if (week == Calendar.SUNDAY) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
        }
        return dateList;
    }

    /**
     * 获取该月的某天到月底的所有星期六
     * @param year 年份
     * @param month 月份
     * @param day 日期
     * @return 日期集合
     */
    public static List getSaturdayInMonthAfterDay(int year, int month,int day) {
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List list = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < daySize-day+1; i++) {
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            if (week == Calendar.SATURDAY ) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
        }
        return dateList;
    }

    /**
     * 获取该月的某天到月底的所有星期日
     * @param year 年份
     * @param month 月份
     * @param day 日期
     * @return 日期集合
     */
    public static List getSundayInMonthAfterDay(int year, int month,int day) {
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List list = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < daySize-day+1; i++) {
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            if (week == Calendar.SUNDAY) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
        }
        return dateList;
    }

    /**
     * 获取该月的某天到月底的所有星期六和星期日
     * @param year 年份
     * @param month 月份
     * @param day 日期
     * @return 日期集合
     */
    public static List getWeekendInMonthAfterDay(int year, int month,int day) {
        List dateList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List list = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        int daySize = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < daySize-day+1; i++) {
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            if (week == Calendar.SATURDAY ) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            if (week == Calendar.SUNDAY) {
                dateList.add(dateFormat.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
        }
        return dateList;
    }
}

你可能感兴趣的:(WeekdayUtil)