Java日历格式

Json格式:

实现代码:

@GetMapping(value="/teacher/week")
    public Map standardweek(HttpServletRequest request,
                              HttpServletResponse response) throws ParseException {
          
        // 返回的结果集
        Map map = new HashMap();
        map.put("year", Integer.valueOf(TimeUtils.getDateString(new Date(), "yyyy")));

        // 存放一周的数据
        List<List<Map>> weekList = new ArrayList<>();

        // 当天时间的00:00:00
        Calendar cal = new GregorianCalendar();
        // 设置周日为第一天
        cal.setFirstDayOfWeek(Calendar.SUNDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
        String todayStr = sdf.format(cal.getTime()); // 2018-09-09T00:00:00+08:00
        // 当天时间的毫秒值
        Long todayLong = sdf.parse(todayStr).getTime();

        // 本周的第几天
        int day_of_week = cal.get(Calendar.DAY_OF_WEEK);

        // 该周是该年的第几周
        int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
        // 开始周
        map.put("startWeeks", week_of_year);
        // 结束周
        map.put("endWeeks", week_of_year + 4);

        // 周日到周六的毫秒值
        Long firstday_of_week = todayLong - (day_of_week - 1) * 24 * 3600000L;
        Long secondday_of_week = firstday_of_week + 24 * 3600000L ;
        Long thirdday_of_week = secondday_of_week + 24 * 3600000L ;
        Long fourday_of_week = thirdday_of_week + 24 * 3600000L ;
        Long fifthday_of_week = fourday_of_week + 24 * 3600000L ;
        Long sixday_of_week = fifthday_of_week + 24 * 3600000L ;
        Long seventh_of_week = sixday_of_week + 24 * 3600000L ;
        Long lastday_of_week = firstday_of_week + 7 * 24 * 3600000L - 1 ;

        // 第一天
        List<Map> firstDay = new ArrayList<>();
        // 第二天
        List<Map> secondDay = new ArrayList<>();
        // 第三天
        List<Map> thirdDay = new ArrayList<>();
        // 第四天
        List<Map> fourthDay = new ArrayList<>();
        // 第五天
        List<Map> fifthDay = new ArrayList<>();
        // 第六天
        List<Map> sixthDay = new ArrayList<>();
        // 第七天
        List<Map> seventhDay = new ArrayList<>();

        // 查询数据库
        List<Map> dblist = teacherAvailableTimeService.selectAvailableTime(Jwt.getID(request));
        if(dblist.size() > 0){
            for (Map m:dblist){
                // 当前数据的startAt(开始时间)
                Date startTime = (Date) m.get("startAt");
                // 判断开始时间是否在本周的范围内(周日-周六)
                if(startTime.getTime() > firstday_of_week && startTime.getTime() < lastday_of_week){
                    // 课程结束时间
                    Date endTime = (Date) m.get("endAt");
                    // 存放课程开始的 hour、minute
                    Map saveStartHourAndMinute = new HashMap();
                    // 存放课程结束的 hour、minute
                    Map saveEndHourAndMinute = new HashMap();
                    // 条数
                    Map<String, Map> number_of_bars = new HashMap();

                    // 课程开始时间 hour:minute
                    String[] startHAndM = (TimeUtils.getDateString(startTime, "HH:mm")).split(":");
                    Integer startHour = Integer.parseInt(startHAndM[0]);
                    Integer startMinute = Integer.parseInt(startHAndM[1]);
                    saveStartHourAndMinute.put("h", startHour);
                    saveStartHourAndMinute.put("m", startMinute);

                    // 课程结束时间 hour:minute
                    String[] endHAndM = (TimeUtils.getDateString(endTime, "HH:mm")).split(":");
                    Integer endHour = Integer.parseInt(endHAndM[0]);
                    Integer endMinute = Integer.parseInt(endHAndM[1]);
                    saveEndHourAndMinute.put("h", endHour);
                    saveEndHourAndMinute.put("m", endMinute);

                    // 第一天00:00:00 - 23:59:59
                    if(startTime.getTime() > firstday_of_week && startTime.getTime() < secondday_of_week){
                        number_of_bars.put("startAt", saveStartHourAndMinute);
                        number_of_bars.put("endAt", saveEndHourAndMinute);
                        // 往firstDay添加条数
                        firstDay.add(firstDay.size(), number_of_bars);

                    } else if(startTime.getTime() > secondday_of_week && startTime.getTime() < thirdday_of_week){
                        // 第二天00:00:00 - 23:59:59
                        number_of_bars.put("startAt", saveStartHourAndMinute);
                        number_of_bars.put("endAt", saveEndHourAndMinute);
                        // 往secondDay添加条数
                        secondDay.add(secondDay.size(), number_of_bars);

                    } else if(startTime.getTime() > thirdday_of_week && startTime.getTime() < fourday_of_week){
                        // 第三天00:00:00 - 23:59:59
                        number_of_bars.put("startAt", saveStartHourAndMinute);
                        number_of_bars.put("endAt", saveEndHourAndMinute);
                        // 往thirdDay添加条数
                        thirdDay.add(thirdDay.size(), number_of_bars);

                    } else if(startTime.getTime() > fourday_of_week && startTime.getTime() < fifthday_of_week){
                        // 第四天00:00:00 - 23:59:59
                        number_of_bars.put("startAt", saveStartHourAndMinute);
                        number_of_bars.put("endAt", saveEndHourAndMinute);
                        // 往fourthDay添加条数
                        fourthDay.add(fourthDay.size(), number_of_bars);

                    } else if(startTime.getTime() > fifthday_of_week && startTime.getTime() < sixday_of_week){
                        // 第五天00:00:00 - 23:59:59
                        number_of_bars.put("startAt", saveStartHourAndMinute);
                        number_of_bars.put("endAt", saveEndHourAndMinute);
                        // 往fifthDay添加条数
                        fifthDay.add(fifthDay.size(), number_of_bars);

                    } else if(startTime.getTime() > sixday_of_week && startTime.getTime() < seventh_of_week){
                        // 第六天00:00:00 - 23:59:59
                        number_of_bars.put("startAt", saveStartHourAndMinute);
                        number_of_bars.put("endAt", saveEndHourAndMinute);
                        // 往sixthDay添加条数
                        sixthDay.add(sixthDay.size(), number_of_bars);

                    } else if(startTime.getTime() > seventh_of_week && startTime.getTime() < lastday_of_week){
                        // 第七天00:00:00 - 23:59:59
                        number_of_bars.put("startAt", saveStartHourAndMinute);
                        number_of_bars.put("endAt", saveEndHourAndMinute);
                        // 往seventhDay添加条数
                        seventhDay.add(seventhDay.size(), number_of_bars);

                    }
                }
            }
        }

        // 往week中添加周日到周一的数据
        weekList.add(0, firstDay);
        weekList.add(1, secondDay);
        weekList.add(2, thirdDay);
        weekList.add(3, fourthDay);
        weekList.add(4, fifthDay);
        weekList.add(5, sixthDay);
        weekList.add(6, seventhDay);

        // 往返回结果集中添加week
        map.put("week", weekList);
        return map;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值