`

生成一年所有周开始,周结束的数据

    博客分类:
  • JAVA
 
阅读更多
public static void main(String args[]) {
        //请注意月份是从0-11,天数是1, 2013-1-1 至 2013-12-31
        Calendar start = Calendar.getInstance();
        start.set(2022, 0, 1); //2022-1-1 开始
        Calendar end = Calendar.getInstance();
        end.set(2023, 0, 0); // 2022-0-0结束,2022-1-1不算

        int sumSunday = 0;
        int sumSaturday = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        int workday = 0 ;
        while(start.compareTo(end) <= 0) {
            int w = start.get(Calendar.DAY_OF_WEEK);
            if(w == Calendar.SUNDAY){
                sumSunday ++;
                workday = 1 ;
            }

            if(w == Calendar.SATURDAY){
                sumSaturday ++;
                workday = 1 ;
            }

            String weekStart = DateUtils.date2String(DateUtils.getBeginDateOfTheWeek(start.getTime()), DateUtils.FORMAT_YYYY_MM_DD) ;
            String weekEnd = DateUtils.date2String(DateUtils.getEndDateOfTheWeek(start.getTime()), DateUtils.FORMAT_YYYY_MM_DD) ;
            //打印每天
            System.out.println(format.format(start.getTime())+","+workday+','+weekStart+","+weekEnd);


            //循环,每次天数加1
            start.set(Calendar.DATE, start.get(Calendar.DATE) + 1);
            workday = 0 ;
        }
        System.out.println("星期天总数为:" + sumSunday);
        System.out.println("星期六总数为:" + sumSaturday);
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics