欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

得到指定年份的所有周末 博客分类: java 周末日期 

程序员文章站 2024-03-16 18:50:04
...
/**
* 得到指定年份的所有周末
*/
public static List<Date> getWeekDayList(int year) {
List<Date> listDate = new ArrayList<Date>();
int i = 1;
        Calendar calendar = new GregorianCalendar(year, 0, 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) {
            listDate.add(calendar.getTime());
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year) {
            listDate.add(calendar.getTime());
            }
        }
        return listDate;
    }
相关标签: 周末 日期