往前推算时间算法示例-java

目标:

根据工作时间,往前推算一定小时时间。

分析思路:

        假设现在时间是13:48
        工作时间是07:00-13:00
        往前推2个小时是多少
        13-2
        12 11
        也就是13-2

        假如是负数,
        其实是flagTime - workStartTime - configTime够不够减,够减直接减,不够继续判断到够
        >=0够减
        <0 那么判断几个workEndTime-workStartTime够,一个够的话,假如是小时,则日期减去1,假如是休息日,则继续减1
        有点像gap+(x-y)*n - value>=0,额
        那循环就完事了,求出n先

/**
     * 往前推算时间代码示例
     * @throws Exception
     */
    private void test10() throws Exception {
        /*
        分析思路:

        假设现在时间是13:48
        工作时间是07:00-13:00
        往前推2个小时是多少
        13-2
        12 11
        也就是13-2

        假如是负数,
        其实是flagTime - workStartTime - configTime够不够减,够减直接减,不够继续判断到够
        >=0够减
        <0 那么判断几个workEndTime-workStartTime够,一个够的话,假如是小时,则日期减去1,假如是休息日,则继续减1
        有点像gap+(x-y)*n - value>=0,额
        那循环就完事了,求出n先

         */

        Date flagTime = new Date(2022 - 1900, 6, 21, 9, 00, 0);
        System.out.println(flagTime);
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        Calendar flagTimeCa = Calendar.getInstance();
        flagTimeCa.setTime(flagTime);

        Date workStartDate = new Date();
        workStartDate.setHours(9);
        workStartDate.setMinutes(0);
        Calendar workStartTimeCalendar = Calendar.getInstance();
        workStartTimeCalendar.setTime(workStartDate);

        Date workEndDate = new Date();
        workEndDate.setHours(12);
        workEndDate.setMinutes(30);
        Calendar workEndTimeCalendar = Calendar.getInstance();
        workEndTimeCalendar.setTime(workEndDate);

        Calendar workStartTimeCalendar2 = Calendar.getInstance();
        workStartTimeCalendar2.setTime(timeFormat.parse("09:00:00"));

        Calendar workEndTimeCalendar2 = Calendar.getInstance();
        workEndTimeCalendar2.setTime(timeFormat.parse("12:30:00"));

        //6个小时
        int hour = 6;

        Calendar resutlCa = Calendar.getInstance();

        boolean flag = true;
        int n = 0;
        while (flag) {
            //如果在工作时间期间,直接减
            if (flagTime.after(workStartDate) && flagTime.before(workEndDate)) {
                if (flagTimeCa.getTimeInMillis() - workStartTimeCalendar.getTimeInMillis() +
                        (workEndTimeCalendar2.getTimeInMillis() - workStartTimeCalendar2.getTimeInMillis()) * n - hour * 60 * 60 * 1000 >= 0) {
                    flag = false;
                    break;
                }
            }
            else {
                if ((workEndTimeCalendar2.getTimeInMillis() - workStartTimeCalendar2.getTimeInMillis()) * n - hour * 60 * 60 * 1000 >= 0) {
                    flag = false;
                    break;
                }
            }


            n++;
        }
        if (n == 0) {
            Date resutlTime = new Date(flagTimeCa.getTimeInMillis() - workStartTimeCalendar.getTimeInMillis());
            resutlCa.setTime(resutlTime);
        }
        if (n > 0) {
            if (flagTime.after(workStartDate) && flagTimeCa.before(workEndDate)) {
                long offset = hour * 60 * 60 * 1000 - flagTimeCa.getTimeInMillis() - workStartTimeCalendar.getTimeInMillis() +
                        -(workEndTimeCalendar2.getTimeInMillis() - workStartTimeCalendar2.getTimeInMillis()) * n;

                resutlCa.setTime(new Date());
                resutlCa.set(flagTime.getYear(), flagTime.getMonth(), flagTime.getDate(), 12, 30, 0);
                resutlCa.add(Calendar.DATE, -n);
                resutlCa.add(Calendar.MILLISECOND, (int) -offset);
            }

            if (flagTime.after(workEndDate)) {
                int dayOffset = n - 1;
                long offset = hour * 60 * 60 * 1000 - (workEndTimeCalendar2.getTimeInMillis() - workStartTimeCalendar2.getTimeInMillis()) * dayOffset;

                resutlCa.setTime(new Date());
                resutlCa.set(flagTime.getYear(), flagTime.getMonth(), flagTime.getDate(), 12, 30, 0);
                resutlCa.add(Calendar.DATE, -dayOffset);
                resutlCa.add(Calendar.MILLISECOND, (int) -offset);
            }

            //假设flagTime在开始时间前,要多减去一天

            if (flagTime.before(workStartDate)) {
                int dayOffset = n - 1;
                long offset = hour * 60 * 60 * 1000 - (workEndTimeCalendar2.getTimeInMillis() - workStartTimeCalendar2.getTimeInMillis()) * dayOffset;

                resutlCa.setTime(new Date());
                resutlCa.set(flagTime.getYear(), flagTime.getMonth(), flagTime.getDate(), 12, 30, 0);
                resutlCa.add(Calendar.DATE, -n);
                resutlCa.add(Calendar.MILLISECOND, (int) -offset);
            }

        }
        System.out.println(new Date(resutlCa.getTimeInMillis()));
    }

你可能感兴趣的:(JAVA工具类,java,开发语言)