diff --git a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java index 310befd..0a9a0c9 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java @@ -9,6 +9,8 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.concurrent.ScheduledExecutorService; @@ -168,45 +170,35 @@ schedules.get(accountId).get(year).put(month, new HashMap<>()); } - SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); - - Schedule lastCreatedSchedule = null; - System.out.println(day); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"); int newScheduleId = nextScheduleId.computeIfAbsent(accountId, k -> 0); - int groupId = newScheduleId; - try { - Date startDate = sdf.parse(startTime); - Date endDate = sdf.parse(endTime); + LocalDateTime startDateTime = LocalDateTime.parse(startTime, formatter); + LocalDateTime endDateTime = LocalDateTime.parse(endTime, formatter); - Calendar scheduleCalendar = Calendar.getInstance(); - scheduleCalendar.setTime(startDate); + LocalDate startDate = startDateTime.toLocalDate(); + LocalDate endDate = endDateTime.toLocalDate(); - while (!scheduleCalendar.getTime().after(endDate)) { - int currentYear = scheduleCalendar.get(Calendar.YEAR); - int currentMonth = scheduleCalendar.get(Calendar.MONTH) + 1; // +1で調整 - int currentDay = scheduleCalendar.get(Calendar.DAY_OF_MONTH); - - // スケジュールの初期化 - schedules.get(accountId).computeIfAbsent(currentYear, k -> new HashMap<>()).computeIfAbsent(currentMonth, k -> new HashMap<>()) - .computeIfAbsent(currentDay, k -> new HashMap<>()); + Schedule res = null; + long days = ChronoUnit.DAYS.between(startDate, endDate) + 1; - Schedule newSchedule = new Schedule(title, startTime, endTime, bookId, groupId); - schedules.get(accountId).get(currentYear).get(currentMonth).get(currentDay).put(groupId, newSchedule); - nextScheduleId.put(accountId, newScheduleId); - lastCreatedSchedule = newSchedule; - - // 次の日に進む - scheduleCalendar.add(Calendar.DAY_OF_MONTH, 1); - } - } catch (ParseException e) { - e.printStackTrace(); // より具体的なエラーハンドリングを検討 - return null; + for(long i = 0; i < days; i++) { + int curYear = startDate.getYear(); + int curMonth = startDate.getMonthValue(); + int curDay = startDate.getDayOfMonth(); + schedules.get(accountId).computeIfAbsent(curYear, k -> new HashMap<>()).computeIfAbsent(curMonth, k -> new HashMap<>()) + .computeIfAbsent(curDay, k -> new HashMap<>()); + Schedule newSchedule = new Schedule(title, startTime, endTime, bookId, newScheduleId); + schedules.get(accountId).get(curYear).get(curMonth).get(curDay).put(newScheduleId, newSchedule); + nextScheduleId.put(accountId, newScheduleId); + startDate = startDate.plusDays(1); + nextScheduleId.put(accountId, newScheduleId + 1); + res = newSchedule; } - nextScheduleId.put(accountId, newScheduleId + 1); - return lastCreatedSchedule; + + return res; } public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId, Integer bookId) {