Develop/Java

[Thymeleaf] temporals.format 으로 날짜 설정하기

롱하 2024. 6. 28. 14:16

 

우선, 프로젝트를 하면서 자주 temporals.format에 대한 정확한 역할을 확인하다가 타임리프의 유틸리티 객체들 중에 날짜 유틸리티 객체인 #temporals에 대해서 알아보고 싶어졌다.

 

- 날짜 유틸리티 객체인 $temporals가 사용된 예시 코드 -

<td class="border-end text-center align-middle"><a th:href="|javascript:goUpdate('${item.id}')|" th:text="${#temporals.format(item.createdAt, 'yyyy-MM-dd hh:mm')}"></a></td>

 

temporals.format은 Java의 날짜 및 시간 API인 java.time 패키지에서 날짜와 시간을 포맷팅하는 데 사용되는 메서드이다.

이 메서드는 java.time.format.DateTimeFormatter 클래스를 사용하여 TemporalAccessor(예: LocalDate, LocalDateTime, ZonedDateTime 등) 객체를 문자열로 포맷팅 하게된다.

 

# 주요 클래스와 인터페이스

 

  • TemporalAccessor: 날짜 또는 시간을 나타내는 객체를 읽기 위한 인터페이스
    • LocalDate, LocalDateTime, ZonedDateTime 등이 이 인터페이스를 구현
  • DateTimeFormatter: 날짜와 시간을 포맷팅하고 파싱하는 데 사용되는 클래스