
Duration ํด๋์ค๋ ๋ ์๊ฐ ์ฌ์ด์ ๊ฐ๊ฒฉ์ ๊ณ์ฐํด์ค๋๋ค. ์๋ ์ฝ๋๋ Comment ํด๋์ค์ inserted๋ผ๋ ์๊ฐ ํ๋๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์ด ํ๋๊ฐ ํ์ฌ๋ก๋ถํฐ ์ผ๋ง๋ ์ง๋ฌ๋์ง๋ฅผ ํ์ํํ์ฌ ๋ฐํํ๋ ๋ฉ์๋๋ฅผ ํฌํจํ๊ณ ์์ต๋๋ค.
@Data
public class Comment {
private LocalDateTime inserted;
public String getInserted() {
LocalDateTime now = LocalDateTime.now();
Duration duration = Duration.between(inserted, now);
long seconds = duration.getSeconds();
long minutes = duration.toMinutes();
long hours = duration.toHours();
long days = duration.toDays();
if (seconds < 60) {
return "๋ฐฉ๊ธ ์ ";
} else if (minutes < 60) {
return minutes + "๋ถ ์ ";
} else if (hours < 24) {
return hours + "์๊ฐ ์ ";
} else if (days < 7) {
return days + "์ผ ์ ";
} else {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return inserted.format(formatter);
}
}
}
inserted๋ ๋๊ธ์ด ์์ฑ๋ ์๊ฐ์ ๋ํ๋
๋๋ค.
ํ์ฌ ์๊ฐ๊ณผ์ ์ฐจ์ด ๊ณ์ฐ : Duration.between๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌinserted ์๊ฐ๊ณผ ํ์ฌ ์๊ฐ(now`) ์ฌ์ด์ ์ฐจ์ด๋ฅผ ๊ณ์ฐํฉ๋๋ค.
์๊ฐ ๋จ์๋ก ๋ณํ : Duration ๊ฐ์ฒด๋ก๋ถํฐ ์ด, ๋ถ, ์๊ฐ, ์ผ ๋จ์๋ก ๋ณํํ์ฌ ๊ฐ๊ฐ์ ๊ฐ์ ๊ตฌํฉ๋๋ค.
์กฐ๊ฑด๋ฌธ์ ์ฌ์ฉํ์ฌ ํด๋นํ๋ ์๊ฐ ๋จ์์ ๋ฐ๋ผ ์ ์ ํ ํ์์ ๋ฌธ์์ด์ ๋ฐํํฉ๋๋ค. ๋ง์ฝ 7์ผ ์ด์ ์ง๋ ๊ฒฝ์ฐ, yyyy-MM-dd ํ์์ ๋ ์ง ๋ฌธ์์ด์ ๋ฐํํฉ๋๋ค.