728x90
프로젝트 진행 중 datetime을 저장하기 위한 타입 간의 설정이 맞지 않아서 애를 먹었다.
처음에는 MySQL: TIMESTAMP, Java: LocalDateTime으로 설정하였는데 MySQL의 timezone을 로컬에서만 서울로 설정해서 문제가 났다.
해결: application.properties의 sql timezone을 Asia/Seoul로 설정
spring.datasource.url=jdbc:[sql주소]?serverTimezone=Asia/Seoul&characterEncoding=UTF-8
이렇게 바꿔주면, timestamp로 저장한 값도 현재 시간으로 잘 설정되고 LocalDateTime.now() 도 문제없이 작동한다.
그렇다면 왜 Java에서 LocalDateTime을 쓰는 게 좋을까?
Java LocalDateTime의 특징
1. 불변 클래스
2. 이전 Date, Calendar 등의 퍼져있던 클래스들을 혼합
3. 잘못된 timezone, 잘못된 날짜 값 설정에 Exception 발생 - 관리가 편리하다.
안정적이고 편리한 날짜 표현을 제공한다.
MySQL Timestamp의 특징
MySQL에서 Timestamp를 사용하는 것의 장점은 다음과 같다.
1. 정확성: Timestamp의 범위는 1970-01-01 00:00:01 ~ 2038-01-19 03:14:07로 datetime보다 정확한 초를 제공한다.
2. 저장 공간: DATETIME보다 저장 공간을 덜 차지한다.
3. 성능: 비교, 정렬에서 DATETIME보다 성능이 좋다.
728x90
'Trouble Shootings' 카테고리의 다른 글
[Spring Boot] Java DateTime Json 입력 시 400 error (0) | 2023.01.29 |
---|---|
[젠킨스/도커] 리눅스 Docker 실행 오류 (0) | 2022.12.26 |
[스프링] Spring messages.properties 한글 인코딩 (0) | 2022.12.23 |
[Vue.js] vue 이미지 src 동적 연결 (v-bind) (0) | 2022.11.20 |
[Vue.js] .env .local 값을 못 읽을 때 (403 error) (0) | 2022.11.20 |