java 工作日设置,周一至周日,周二至周日等等

需要设置工作日期,例如:周一至周日,周二至周日等等。

// 准备测试数据
public static void main(String[] args) {
        List<Object> list = new ArrayList<>();
        String[] day = {"周二","周三","周四","周五","周六","周日"};
        list.add(day);
        day = new String[]{"周一", "周二", "周三"};
        list.add(day);
        day = new String[]{"周五", "周六", "周日"};
        list.add(day);
        day = new String[]{"周三", "周四", "周五", "周六", "周日"};
        list.add(day);
        day = new String[]{"周一", "周二", "周五", "周六", "周日"};
        list.add(day);
        day = new String[]{"周一", "周二", "周三", "周四", "周五"};
        list.add(day);
        day = new String[]{"周二", "周三", "周四", "周五", "周六", "周日"};
        list.add(day);
        day = new String[]{"周一", "周二", "周三", "周五", "周六"};
        list.add(day);
        day = new String[]{"周一","周三", "周四", "周五", "周六"};
        list.add(day);
        day = new String[]{"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        list.add(day);
        day = new String[]{"周一", "周六", "周日"};
        list.add(day);
        for (Object s : list) {
            System.out.println(checkWorkingDay((String[]) s));
        }
    }
    // 重新编排工作日
    public static String checkWorkingDay(String[] day){
        String workingDay = "";
        if (day.length > 6){
            workingDay = "周一至周日";
        } else if (day.length == 6){
            if ("周六".equals(day[day.length-1])&&"周一".equals(day[0])){
                workingDay = "周一至周六";
            } else if ("周日".equals(day[day.length-1])&&"周二".equals(day[0])){
                workingDay = "周二至周日";
            } else {
                for (int i = 0; i < day.length; i++) {
                    if (i==day.length-1){
                        workingDay += day[i];
                    } else {
                        workingDay += day[i]+",";
                    }
                }
            }
        } else if (day.length == 5){
            if ("周一".equals(day[0]) && "周五".equals(day[day.length-1])){
                workingDay = "周一至周五";
            } else if ("周二".equals(day[0]) && "周六".equals(day[day.length-1])){
                workingDay = "周二至周六";
            } else if ("周三".equals(day[0]) && "周日".equals(day[day.length-1])){
                workingDay = "周三至周日";
            } else {
                for (int i = 0; i < day.length; i++) {
                    if (i==day.length-1){
                        workingDay += day[i];
                    } else {
                        workingDay += day[i]+",";
                    }
                }
            }
        } else if (day.length == 4){
            if ("周一".equals(day[0]) && "周四".equals(day[day.length-1])){
                workingDay = "周一至周四";
            } else if ("周二".equals(day[0]) && "周五".equals(day[day.length-1])){
                workingDay = "周二至周五";
            } else if ("周三".equals(day[0]) && "周六".equals(day[day.length-1])){
                workingDay = "周三至周六";
            } else if ("周四".equals(day[0]) && "周日".equals(day[day.length-1])){
                workingDay = "周四至周日";
            } else {
                for (int i = 0; i < day.length; i++) {
                    if (i==day.length-1){
                        workingDay += day[i];
                    } else {
                        workingDay += day[i]+",";
                    }
                }
            }
        } else if (day.length == 3){
            if ("周一".equals(day[0]) && "周三".equals(day[day.length-1])){
                workingDay = "周一至周三";
            } else if ("周二".equals(day[0]) && "周四".equals(day[day.length-1])){
                workingDay = "周二至周四";
            } else if ("周三".equals(day[0]) && "周五".equals(day[day.length-1])){
                workingDay = "周三至周五";
            }else if ("周四".equals(day[0]) && "周六".equals(day[day.length-1])){
                workingDay = "周四至周日";
            }else if ("周五".equals(day[0]) && "周日".equals(day[day.length-1])){
                workingDay = "周五至周日";
            } else {
                for (int i = 0; i < day.length; i++) {
                    if (i==day.length-1){
                        workingDay += day[i];
                    } else {
                        workingDay += day[i]+",";
                    }
                }
            }
        } else {
            for (int i = 0; i < day.length; i++) {
                if (day.length == 1){
                    workingDay += day[i];
                } else if (day.length-1==i){
                    workingDay += day[i];
                }
                else {
                    workingDay += day[i]+",";
                }
            }
        }
        return workingDay;
    }

输出结果:

你可能感兴趣的