-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: 크롤링 데이터 저장 시, datetime으로 변환되어 저장되도록 변경
- Loading branch information
Showing
3 changed files
with
57 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/cmc/suppin/event/crawl/converter/DateConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.cmc.suppin.event.crawl.converter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class DateConverter { | ||
|
||
public static LocalDateTime convertRelativeTime(String relativeTime) { | ||
LocalDateTime now = LocalDateTime.now(); | ||
if (relativeTime.contains("일 전")) { | ||
int days = extractNumber(relativeTime); | ||
return now.minusDays(days); | ||
} else if (relativeTime.contains("시간 전")) { | ||
int hours = extractNumber(relativeTime); | ||
return now.minusHours(hours); | ||
} else if (relativeTime.contains("분 전")) { | ||
int minutes = extractNumber(relativeTime); | ||
return now.minusMinutes(minutes); | ||
} else if (relativeTime.contains("주 전")) { | ||
int weeks = extractNumber(relativeTime); | ||
return now.minusWeeks(weeks); | ||
} else if (relativeTime.contains("개월 전")) { | ||
int months = extractNumber(relativeTime); | ||
return now.minusMonths(months); | ||
} else if (relativeTime.contains("년 전")) { | ||
int years = extractNumber(relativeTime); | ||
return now.minusYears(years); | ||
} | ||
return now; | ||
} | ||
|
||
private static int extractNumber(String relativeTime) { | ||
Pattern pattern = Pattern.compile("\\d+"); | ||
Matcher matcher = pattern.matcher(relativeTime); | ||
if (matcher.find()) { | ||
return Integer.parseInt(matcher.group()); | ||
} | ||
return 0; | ||
} | ||
|
||
public static String formatLocalDateTime(LocalDateTime dateTime) { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); | ||
return dateTime.format(formatter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters