Spring :: 날짜/시간 자동 포맷팅

2026. 2. 11. 21:41백엔드/Spring Framework

LocalDateFormatter.java

package org.zerock.mallapi.controller.formatter;

import org.springframework.format.Formatter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

// 날짜/시간 주의
// 브라우저 : 문자열로 전송
// 서버 : LocalDate 또는 LocalDateTime 으로 처리함
// 그러므로 이를 자동으로 변환해주는 이 클래스를 정의한다.
// 이후 스프링 MVC 동작 과정에서 사용될 수 있도록 설정을 추가해주어야 함.
public class LocalDateFormatter implements Formatter<LocalDate> {
    @Override
    public LocalDate parse(String text, Locale locale) {
        return LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    @Override
    public String print(LocalDate object, Locale locale) {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(object);
    }

}

 

→ 이것이 MVC 동작 과정에서 사용될 수 있도록 설정을 추가해 주어야 함

→ CustomServletConfig

 

 

CustomServletConfig.java

package org.zerock.mallapi.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.zerock.mallapi.controller.formatter.LocalDateFormatter;

@Configuration
public class CustomServletConfig implements WebMvcConfigurer {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new LocalDateFormatter());
    }
}

 

'백엔드 > Spring Framework' 카테고리의 다른 글

Spring :: 페이징 처리 (요약)  (0) 2026.02.11
Spring :: EntityManager  (3) 2025.08.05
JDBC :: 내부 구조 파헤치기  (3) 2025.08.04
Spring :: 주요 모듈  (2) 2025.08.04
Spring :: 프로젝트 관리 :: Maven, Gradle  (2) 2025.08.04