方法 getWeeks获取当前月自然周数

代码一:

  /**
     * 获取当前月自然周数,并返回每周开始日期和每周结束日期
     * @param date 2013-9 : 1-1,2-8,9-15,16-22,23-29,30-30
     * @return
     * @throws Exception
     */
    public static String getWeeks(String date){
        StringBuilder sb = new StringBuilder("");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Date date1 = null;
        try {
            date1 = dateFormat.parse(date);
        } catch (ParseException e) {
            System.out.println("获取当前月自然周,日期格式转换错误!11");
            e.printStackTrace();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date1);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int count = 0;
        for (int i = 1; i <= days; i++) {
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date date2 = null;
            try {
                date2 = dateFormat1.parse(date + "-" + i);
            } catch (ParseException e) {
                System.out.println("获取当前月自然周,日期格式转换错误!22");
                e.printStackTrace();
            }
            calendar.clear();
            calendar.setTime(date2);
            int k = new Integer(calendar.get(Calendar.DAY_OF_WEEK));
            int startDay = 0;
            int endDay = 0;
            // 若当天是周日
            if (k == 1) {
                count++;
                if (i - 6 <= 1) {
                    startDay = 1;
                } else {
                    startDay = i - 6;
                }
                endDay = i;
            }
            // 若是本月最好一天,且不是周日
            if (k != 1 && i == days) {
                count++;
                startDay = i - k + 2;
                endDay = i;
            }
            if(startDay != 0 && endDay != 0){
                if(sb.toString() == null || "".equals(sb.toString())){
                    sb.append(startDay + "-" + endDay);
                }else{
                    sb.append("," + startDay + "-" + endDay);
                }
            }
        }
        System.out.println(date + "共计" + count + "周,结果=" + sb.toString());
        return sb.toString();
    }

输出一:

getWeeks("2013-9");
getWeeks("2019-08");

2013-9共计6周,结果=1-1,2-8,9-15,16-22,23-29,30-30
2019-08共计5周,结果=1-4,5-11,12-18,19-25,26-31

代码二(重写):

    /**
     * 将list分割后拼接成字符串
     * @param list
     * @param separator
     * @return
     */
    public static String listToString(List<String> list, String separator) {
        StringBuilder builder = new StringBuilder();
        if(!CollectionUtils.isEmpty(list)){
            for (String val : list) {
                if(builder.length() > 0){
                    builder.append(separator);
                }
                builder.append(val);
            }
        }
        return builder.toString();
    }
    
    /**
     * 获取当前月自然周数,并返回每周开始日期和每周结束日期(重写)
     * @param date 2013-9 : 1-1,2-8,9-15,16-22,23-29,30-30
     * @return
     * @throws Exception
     */
    public static String getWeeks(String time) throws ParseException{
        SimpleDateFormat formatYM = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");
        Date date = formatYM.parse(time);
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONDAY) + 1;
        
        int j = 0;
        int start = 1;
        int end;
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= days; i++) {
            calendar.setTime(formatYMD.parse(time + "-" + i));
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            
            end = i;// 实时记录可能的一周结束时间
            
            // 周日或当月最后一天统计计数
            if(dayOfWeek == 1 || end >= days){
                list.add(start + "-" + end);
//                System.out.println("第" + j + "周:" + start + "-" + end);
                ++ j;
            }
            
            // 周日返回1,周六返回7
            if(dayOfWeek == 1){// 周日重新记录一周开始时间
                start = i + 1;
            }
        }
        
        String rev = listToString(list, ",");
        System.out.println(year + "" + month + "月有" + days + "天,共计" + j + "周。" + rev);
        return rev;
    }

输出二:

getWeeks("2013-9");
getWeeks("2019-08");

2013年9月有30天,共计6周。1-1,2-8,9-15,16-22,23-29,30-30
2019年8月有31天,共计5周。1-4,5-11,12-18,19-25,26-31

ps(吐槽):

奇葩方法来源于奇葩需求,奇葩需求来源于奇葩业务!!
posted @ 2019-08-27 19:06  王晓鸣  阅读(603)  评论(0)    收藏  举报