`

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

    博客分类:
  • JAVA
 
阅读更多
public static void main(String[] args) throws Exception {
        String year = "2022";
        Calendar c = Calendar.getInstance();
        c.set(Integer.parseInt(year), 0, 1);
        Calendar c2 = Calendar.getInstance();
        c2.set(Integer.parseInt(year) + 1, 0, 1);
        int count = 0;
        while (c.compareTo(c2) < 0) {
            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
                String str = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) ;
                Date dateStart = DateUtils.convertStringDate(str, DateUtils.FORMAT_YYYY_MM_DD) ;
                String dateStartStr = DateUtils.date2String(dateStart, DateUtils.FORMAT_YYYY_MM_DD) ;
                System.out.print(dateStartStr+",");
            }

            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                String str = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) ;
                Date dateEnd = DateUtils.convertStringDate(str, DateUtils.FORMAT_YYYY_MM_DD) ;
                String dateEndStr = DateUtils.date2String(dateEnd, DateUtils.FORMAT_YYYY_MM_DD) ;
                System.out.println(dateEndStr);

                count++;
            }
            // 日期+1
            c.add(Calendar.DATE, 1);
        }
        System.out.println("共" + count + "条记录");
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics