输入日期字符串(yyyy-MM-dd),得到当月周数

Java输入一个日期字符串,返回当月第几周数

  • 以第一个周一开始计算,即1号不是周一的算上个月最后一周
    • po代码
  • 只需要带入日期字符串(yyyy-MM-dd)参数,即返回当月的第几周

以第一个周一开始计算,即1号不是周一的算上个月最后一周

本来想写JS代码,网上找了很多方法都不行,最后写了Java版本的代码。
前提:以第一个周一开始计算,即1号不是周一的算上个月最后一周,1号是周一,则是当月第一周。

po代码

    /**
     * 输入日期字符串,获取为当月第几周(***当月第一个周一开始算***)
     * @param dayOfMonth
     * @return
     * @throws ParseException
     */
    public static int getWeekOfMonth(String dayOfMonth) throws ParseException {
        String firstDayOfMonth = getFirstDay(dayOfMonth);
        int lastIntDay = Integer.parseInt(dayOfMonth.split("-")[2]);
        int firstIntDay = Integer.parseInt(firstDayOfMonth.split("-")[2]);
        Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(firstDayOfMonth);
        String dayOfWeek = new SimpleDateFormat("EEEE").format(date1);
        switch (dayOfWeek) {
            case "星期一":
                return (lastIntDay - firstIntDay) / 7 + 1;
            case "星期二":
                return (lastIntDay - firstIntDay - 6) / 7 + 1;
            case "星期三":
                return (lastIntDay - firstIntDay - 5) / 7 + 1;
            case "星期四":
                return (lastIntDay - firstIntDay - 4) / 7 + 1;
            case "星期五":
                return (lastIntDay - firstIntDay - 3) / 7 + 1;
            case "星期六":
                return (lastIntDay - firstIntDay - 2) / 7 + 1;
            case "星期日":
                return (lastIntDay - firstIntDay - 1) / 7 + 1;
        }
        return 0;
    }
    //如果jre所在的系统是Linux则上面的方法会出错,用以下通用方法
        /**public static int getWeekOfMonth(String dayOfMonth) throws ParseException {
        String firstDayOfMonth = getFirstDay(dayOfMonth);
        int lastIntDay = Integer.parseInt(dayOfMonth.split("-")[2]);
        int firstIntDay = Integer.parseInt(firstDayOfMonth.split("-")[2]);
        Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(firstDayOfMonth);
        // String dayOfWeek = new SimpleDateFormat("EEEE").format(date1);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 2:
                return (lastIntDay - firstIntDay) / 7 + 1; //星期一
            case 3:
                return (lastIntDay - firstIntDay - 6) / 7 + 1;//星期二
            case 4:
                return (lastIntDay - firstIntDay - 5) / 7 + 1; //星期三
            case 5:
                return (lastIntDay - firstIntDay - 4) / 7 + 1; //星期四
            case 6:
                return (lastIntDay - firstIntDay - 3) / 7 + 1; //星期五
            case 7:
                return (lastIntDay - firstIntDay - 2) / 7 + 1; //星期六
            case 1:
                return (lastIntDay - firstIntDay - 1) / 7 + 1; //星期日
        }
        return 0;
    }*/


    public static String getFirstDay(String theDay) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        Date date = null;
        try {
            date = format.parse(theDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cale.setTime(date);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }

只需要带入日期字符串(yyyy-MM-dd)参数,即返回当月的第几周

你可能感兴趣的:(Java,当月周数)