본문 바로가기

Spring Boot

날짜 표시하기(오늘기준 몇 분, 시간, 일, 달, 시간 전)

Period - Year, Month, Day

Duration - seconds, nano seconds 

 

@Data
public class Board {

    private Integer id;
    private String title;
    private String content;
    private String writer;
    private String nickname;
    private Integer countComment;
    private Integer countLike;
    private LocalDateTime inserted;

    public String getAgo(){

        // 현재 시간
        LocalDateTime now = LocalDateTime.now();

        if (inserted.isBefore(now.minusYears(1))) {
            Period between = Period.between(inserted.toLocalDate(), now.toLocalDate());
            return between.get(ChronoUnit.YEARS) + "년 전";
        } else if (inserted.isBefore(now.minusMonths(1))) {
            Period between = Period.between(inserted.toLocalDate(), now.toLocalDate());
            return between.get(ChronoUnit.MONTHS) + "달 전";
        } else if (inserted.isBefore(now.minusDays(1))) {
            Period between = Period.between(inserted.toLocalDate(), now.toLocalDate());
            return between.get(ChronoUnit.DAYS) + "일 전";
        } else if (inserted.isBefore(now.minusHours(1))) {
            Duration between = Duration.between(inserted, now);
            return (between.getSeconds() / 60 / 60) + "시간 전";
        } else if (inserted.isBefore(now.minusMinutes(1))) {
            Duration between = Duration.between(inserted, now);
            return (between.getSeconds() / 60) + "분 전";
        } else {
            Duration between = Duration.between(inserted, now);
            return between.getSeconds() + "초 전";
        }

    }
}

 

 

근데 이렇게하면 지저분하니깐 따로 파일을 빼서 static 메소드로 생성 후 꺼내 쓰기

코드 분
사용

 

근데 어차피 두번째 파라미터는 고정이니깐 저장된 시간 값만 파라미터로 받으면됨

public class AppUtil {

    public static String getAgo(LocalDateTime a) {

        if (a == null) {
            return "";
        }

        // 서울 시간
        LocalDateTime b = LocalDateTime.now(ZoneId.of("Asia/Seoul"));

        if (a.isBefore(b.minusYears(1))) {
            Period between = Period.between(a.toLocalDate(), b.toLocalDate());
            return between.get(ChronoUnit.YEARS) + "년 전";
        } else if (a.isBefore(b.minusMonths(1))) {
            Period between = Period.between(a.toLocalDate(), b.toLocalDate());
            return between.get(ChronoUnit.MONTHS) + "달 전";
        } else if (a.isBefore(b.minusDays(1))) {
            Period between = Period.between(a.toLocalDate(), b.toLocalDate());
            return between.get(ChronoUnit.DAYS) + "일 전";
        } else if (a.isBefore(b.minusHours(1))) {
            Duration between = Duration.between(a, b);
            return (between.getSeconds() / 60 / 60) + "시간 전";
        } else if (a.isBefore(b.minusMinutes(1))) {
            Duration between = Duration.between(a, b);
            return (between.getSeconds() / 60) + "분 전";
        } else {
            Duration between = Duration.between(a, b);
            return between.getSeconds() + "초 전";
        }
    }

}

'Spring Boot' 카테고리의 다른 글

spring actuator  (0) 2024.05.08
ResponseEntity - 응답코드, 응답본문, 응답헤더 작성 가능한 객체  (0) 2023.10.25
파일 첨부(전송, 받기)  (0) 2023.10.20
의존성 주입 DI (Dependency Inject)  (0) 2023.10.17
@Compnent  (0) 2023.10.17