Server/Spring Boot
Springboot Timezone(타임존) 에 대해 알아보자!
hoonylab
2025. 4. 15. 00:01
728x90
반응형
시간은 모든 시스템에서 매우 중요한 요소입니다. 특히 서버와 클라이언트가 서로 다른 시간대를 사용할 경우, 정확한 시간 처리를 위해 타임존 개념과 변환 방법을 제대로 이해하는 것이 중요합니다. 이번 글에서는 KST(한국 표준시)와 UTC(세계 표준시)의 개념 비교부터 Spring Boot에서의 변환 예제까지 전부 정리해보겠습니다.
🕒 KST vs UTC
구분 | 설명 |
---|---|
UTC (Coordinated Universal Time) | 전 세계 표준 시간. 한국 시간(KST)은 UTC보다 9시간 빠름 |
KST (Korea Standard Time) | 한국 표준 시간 (UTC +09:00) |
📌 예시:
- UTC 기준: 2025-04-14 06:00:00
- KST 기준: 2025-04-14 15:00:00
⚙️ Spring Boot에서 타임존 설정하기
1. JVM 옵션 설정
-Duser.timezone=Asia/Seoul
예:
java -Duser.timezone=Asia/Seoul -jar myapp.jar
2. 코드에서 설정
@PostConstruct
public void setTimeZone() {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
}
3. application.yml / application.properties 설정
spring:
jackson:
time-zone: Asia/Seoul
🔄 KST ↔ UTC 변환 코드 예제 (Java)
✅ KST → UTC
import java.time.*;
public class TimezoneExample {
public static void main(String[] args) {
LocalDateTime kstTime = LocalDateTime.of(2025, 4, 14, 15, 0); // 예시
ZoneId kstZone = ZoneId.of("Asia/Seoul");
ZonedDateTime kstZoned = kstTime.atZone(kstZone);
ZonedDateTime utcZoned = kstZoned.withZoneSameInstant(ZoneOffset.UTC);
System.out.println("KST 시간 : " + kstZoned);
System.out.println("UTC 시간 : " + utcZoned);
}
}
🖨️ 출력 예시
KST 시간 : 2025-04-14T15:00+09:00[Asia/Seoul]
UTC 시간 : 2025-04-14T06:00Z[UTC]
✅ UTC → KST
import java.time.*;
public class TimezoneExample {
public static void main(String[] args) {
LocalDateTime utcTime = LocalDateTime.of(2025, 4, 14, 6, 0); // 예시
ZoneId utcZone = ZoneOffset.UTC;
ZonedDateTime utcZoned = utcTime.atZone(utcZone);
ZonedDateTime kstZoned = utcZoned.withZoneSameInstant(ZoneId.of("Asia/Seoul"));
System.out.println("UTC 시간 : " + utcZoned);
System.out.println("KST 시간 : " + kstZoned);
}
}
🖨️ 출력 예시
UTC 시간 : 2025-04-14T06:00Z[UTC]
KST 시간 : 2025-04-14T15:00+09:00[Asia/Seoul]
⏱️ ZonedDateTime / ZoneOffset 포맷팅 예제
✅ 포맷을 적용해 보기
import java.time.*;
import java.time.format.DateTimeFormatter;
public class FormatExample {
public static void main(String[] args) {
ZonedDateTime nowKST = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
ZonedDateTime nowUTC = nowKST.withZoneSameInstant(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
System.out.println("KST 포맷 : " + nowKST.format(formatter));
System.out.println("UTC 포맷 : " + nowUTC.format(formatter));
}
}
🖨️ 출력 예시
KST 포맷 : 2025-04-14 15:00:00 KST
UTC 포맷 : 2025-04-14 06:00:00 UTC
✅ 마무리 요약
항목 | 내용 |
---|---|
🌐 타임존 기본 | KST는 UTC보다 9시간 빠름 |
🛠️ Spring Boot 설정 | JVM 옵션, 코드, YML 설정 가능 |
🔄 시간 변환 | ZonedDateTime.withZoneSameInstant(...) 사용 |
🧾 포맷 출력 | DateTimeFormatter로 원하는 형식 지정 가능 |
Spring Boot와 Java 8의 java.time
API를 활용하면 타임존 변환이 매우 간단하고 직관적입니다. 서버 로직에서 UTC를 기준으로 처리하고, 클라이언트에선 로컬 타임(KST 등)을 보여주는 구조를 설계하면 안정적인 시간 처리가 가능합니다.
728x90
반응형