From 511f32ae768b449b25b41cac4a7e7d6406aeafe7 Mon Sep 17 00:00:00 2001 From: jihyeon baek Date: Wed, 3 Jul 2024 22:22:41 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=97=B0=EC=9D=B8=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=20=EB=B0=98=ED=99=98=201=EA=B0=9C=EB=A7=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AnniversaryService.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/example/fiurinee/domain/anniversary/service/AnniversaryService.java b/src/main/java/com/example/fiurinee/domain/anniversary/service/AnniversaryService.java index 0ab1176..cd97df4 100644 --- a/src/main/java/com/example/fiurinee/domain/anniversary/service/AnniversaryService.java +++ b/src/main/java/com/example/fiurinee/domain/anniversary/service/AnniversaryService.java @@ -108,34 +108,47 @@ public List> calculateDDay(Anniversary anniversary) { LocalDate today = LocalDate.now(); LocalDateTime anniversaryDateTime = anniversary.getAnniversaryDate().toLocalDateTime(); LocalDate anniversaryDate = anniversaryDateTime.toLocalDate(); - long yearsDifference = ChronoUnit.YEARS.between(anniversaryDate, today); + + Map closestDDay = null; + int minDays = Integer.MAX_VALUE; if (anniversary.getType() == AnniversaryType.연인) { int daysPassed = (int) ChronoUnit.DAYS.between(anniversaryDate, today); int nextDay = ((daysPassed / 100) + 1) * 100; - for (int i = 0; i < 1; i++) { + for (int i = 0; i < 1; i++) { // Only find the next closest 100-day anniversary LocalDate hundredDays = anniversaryDate.plusDays(nextDay + i * 100); if (!hundredDays.isBefore(today)) { - Map dDay = new HashMap<>(); - dDay.put((nextDay + i * 100) + "days", (int) ChronoUnit.DAYS.between(today, hundredDays)-1); - dDayList.add(dDay); + int daysToHundredDays = (int) ChronoUnit.DAYS.between(today, hundredDays) - 1; + if (daysToHundredDays < minDays) { + minDays = daysToHundredDays; + closestDDay = new HashMap<>(); + closestDDay.put((nextDay + i * 100) + "days", daysToHundredDays); + } break; } } } - + long yearsDifference = ChronoUnit.YEARS.between(anniversaryDate, today); for (int i = 1; i <= yearsDifference + 1; i++) { LocalDate yearAnniversary = anniversaryDate.plusYears(i); if (!yearAnniversary.isBefore(today)) { - Map dDay = new HashMap<>(); - dDay.put("year", (int) ChronoUnit.DAYS.between(today, yearAnniversary)); - dDayList.add(dDay); + int daysToYearAnniversary = (int) ChronoUnit.DAYS.between(today, yearAnniversary); + if (daysToYearAnniversary < minDays) { + minDays = daysToYearAnniversary; + closestDDay = new HashMap<>(); + closestDDay.put("year", daysToYearAnniversary); + } break; } } + if (closestDDay != null) { + dDayList.add(closestDDay); + } + + return dDayList; }