1. 程式人生 > >Java日曆格式

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;
    }