From 18cf7d1a62adf8d370b4a579fad68355e3ceea37 Mon Sep 17 00:00:00 2001 From: dyhj625 Date: Fri, 11 Apr 2025 10:18:55 +0900 Subject: [PATCH] =?UTF-8?q?=EB=82=A0=EC=94=A8=20=EB=A9=94=EC=9D=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/WeatherController.java | 112 +++++++++++------- 1 file changed, 70 insertions(+), 42 deletions(-) diff --git a/src/main/java/io/company/localhost/controller/common/WeatherController.java b/src/main/java/io/company/localhost/controller/common/WeatherController.java index c5ee541..d7bd915 100644 --- a/src/main/java/io/company/localhost/controller/common/WeatherController.java +++ b/src/main/java/io/company/localhost/controller/common/WeatherController.java @@ -1,5 +1,9 @@ package io.company.localhost.controller.common; +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -65,62 +69,86 @@ public class WeatherController { JsonNode nodeData = objectMapper.readTree(jsonData); JsonNode forecastList = nodeData.get("list"); - // 날짜별로 데이터 그룹화 + // 현재 날짜 및 시간 계산 + LocalDate today = LocalDate.now(); + LocalDateTime nowTime = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + // 날짜별 데이터를 그룹화 Map> dailyWeathers = new HashMap<>(); Map> dailyDescriptions = new HashMap<>(); Map> dailyIcons = new HashMap<>(); + // 오늘 예보 중 가장 가까운 예보 저장 변수 + JsonNode closestForecastToday = null; + long minDiff = Long.MAX_VALUE; + + // forecastList를 순회하며 데이터를 그룹화하고, 오늘의 경우 가장 가까운 forecast 선택 for (JsonNode forecast : forecastList) { - String date = forecast.get("dt_txt").asText().split(" ")[0]; //날짜 추출 + String dtTxt = forecast.get("dt_txt").asText(); + String date = dtTxt.split(" ")[0]; // 날짜 추출 (예: "2025-04-04") - JsonNode weather = forecast.get("weather").get(0); + JsonNode weather = forecast.get("weather").get(0); String mainWeather = weather.get("main").asText(); String description = weather.get("description").asText(); String icon = weather.get("icon").asText(); - if (!dailyWeathers.containsKey(date)) { - dailyWeathers.put(date, new ArrayList<>()); - dailyDescriptions.put(date, new ArrayList<>()); - dailyIcons.put(date, new ArrayList<>()); - } + // 해당 날짜의 리스트에 추가 (map이 없으면 생성) + dailyWeathers.computeIfAbsent(date, k -> new ArrayList<>()).add(mainWeather); + dailyDescriptions.computeIfAbsent(date, k -> new ArrayList<>()).add(description); + dailyIcons.computeIfAbsent(date, k -> new ArrayList<>()).add(icon); - dailyWeathers.get(date).add(mainWeather); - dailyDescriptions.get(date).add(description); - dailyIcons.get(date).add(icon); - } - - // 각 날짜별 대표 날씨 결정 (가장 빈번한 날씨 상태) - List dailyWeatherList = new ArrayList<>(); - - for (String date : dailyWeathers.keySet()) { - List weathers = dailyWeathers.get(date); - List descriptions = dailyDescriptions.get(date); - List icons = dailyIcons.get(date); - - // 가장 빈번한 날씨 찾기(카운팅) - Map weatherCounts = new HashMap<>(); - for (String w : weathers) { - weatherCounts.put(w, weatherCounts.getOrDefault(w, 0) + 1); - } - - String mainWeather = ""; - int maxCount = 0; - - for (Map.Entry entry : weatherCounts.entrySet()) { - if (entry.getValue() > maxCount) { - maxCount = entry.getValue(); - mainWeather = entry.getKey(); + // 오늘 날짜의 forecast라면 현재 시간과의 차이 계산 + if (date.equals(today.toString())) { + LocalDateTime forecastTime = LocalDateTime.parse(dtTxt, formatter); + long diff = Math.abs(Duration.between(forecastTime, nowTime).toMillis()); + if (diff < minDiff) { + minDiff = diff; + closestForecastToday = forecast; } } - - // 평균값 계산 - int mainWeatherIndex = weathers.indexOf(mainWeather); - String description = descriptions.get(mainWeatherIndex); - String icon = icons.get(mainWeatherIndex); - - dailyWeatherList.add(new WeatherVo(date, mainWeather, description, icon)); } - + + // 최종적으로 각 날짜의 대표 날씨 결정 + List dailyWeatherList = new ArrayList<>(); + for (String date : dailyWeathers.keySet()) { + String repMainWeather; + String repDescription; + String repIcon; + + // 오늘인 경우, 가장 가까운 forecast의 아이콘과 설명 사용 + if (date.equals(today.toString()) && closestForecastToday != null) { + JsonNode weather = closestForecastToday.get("weather").get(0); + repMainWeather = weather.get("main").asText(); + repDescription = weather.get("description").asText(); + repIcon = weather.get("icon").asText(); + } else { + // 기타 날짜는 기존 방식대로 가장 빈번한 날씨 상태 선택 + List weathers = dailyWeathers.get(date); + List descriptions = dailyDescriptions.get(date); + List icons = dailyIcons.get(date); + + Map weatherCounts = new HashMap<>(); + for (String w : weathers) { + weatherCounts.put(w, weatherCounts.getOrDefault(w, 0) + 1); + } + + repMainWeather = ""; + int maxCount = 0; + for (Map.Entry entry : weatherCounts.entrySet()) { + if (entry.getValue() > maxCount) { + maxCount = entry.getValue(); + repMainWeather = entry.getKey(); + } + } + int repIndex = weathers.indexOf(repMainWeather); + repDescription = descriptions.get(repIndex); + repIcon = icons.get(repIndex); + } + + dailyWeatherList.add(new WeatherVo(date, repMainWeather, repDescription, repIcon)); + } + return dailyWeatherList; }