根据日期获取周几,根据时间段获取几到周几

**根据日期获取几**
 public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

	**根据时间段获取几**
    public static int dateToWeekNum(Date datetime) {
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        cal.setTime(datetime);
        int w =  cal.get(Calendar.DAY_OF_WEEK)-2;
        if (w < 0)
            w = 6;
        return w;
    }

    **根据时间段获取几到周几**
    public static List calcDateInterval(Date minDate, Date maxDate) {
        List rangeList =new ArrayList<>();
        if(DateUtil.between(minDate, maxDate, DateUnit.DAY)>=7){
            int[] range ={0, 1, 2, 3, 4, 5, 6};
            rangeList = Arrays.stream(range).boxed().collect(Collectors.toList());
        }else {
            DateRange dateRange = DateUtil.range(minDate, maxDate, DateField.DAY_OF_WEEK);
            for (DateTime dateTime : dateRange) {
                rangeList.add(dateToWeekNum(dateTime));
            }
        }
        return rangeList;
    }

你可能感兴趣的:(java,前端,服务器)