안드로이드에서 현재 시간을 KST(한국 표준시)로 표시하기
개발/안드로이드1 2025. 10. 24. 19:53안드로이드에서 시간을 다룰 때 대부분 다음과 같이 작성합니다
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Seoul"));
String currentTime = sdf.format(new Date());
이 방식은 간단하지만, SimpleDateFormat은 스레드 안전하지 않고, 오래된 API라 최신 안드로이드 코드에서는 더 이상 권장되지 않습니다.
=> 대신 java.time 패키지의 DateTimeFormatter와 ZonedDateTime을 사용하면 안전하고 가독성 높은 코드를 작성할 수 있습니다.
Java 버전 예제
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneId;
public class KSTTimeExample {
public static void main(String[] args) {
// KST(한국 표준시) 기준 현재 시간
ZonedDateTime kstNow = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
// 원하는 포맷 지정
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 포맷팅된 문자열로 변환
String formattedTime = kstNow.format(formatter);
System.out.println("현재 시간 (KST): " + formattedTime);
}
}
설명
- ZoneId.of("Asia/Seoul") → 한국 표준시 적용
- ZonedDateTime.now() → 현재 시간을 즉시 가져옴
- DateTimeFormatter → 포맷 형태 지정 가능 (HHmm, yyyyMMdd, a hh:mm 등)
예시 출력:
현재 시간 (KST): 2025-10-24 14:35:12
Kotlin 버전 예제
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.ZoneId
fun main() {
// KST 시간대의 현재 시간
val kstNow = ZonedDateTime.now(ZoneId.of("Asia/Seoul"))
// 포맷 지정
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
// 문자열로 포맷팅
val formattedTime = kstNow.format(formatter)
println("현재 시간 (KST): $formattedTime")
}
Kotlin은 코드가 훨씬 간결하고 함수형 스타일로 로그 출력에도 바로 활용할 수 있습니다.
하위 버전(API 26 미만) 지원 방법
java.time은 API 26(Oreo) 이상에서 기본 제공됩니다.
만약 그 이하 버전도 지원해야 한다면 아래와 같이 구현하세요.
Java 버전
import android.os.Build;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class TimeExample {
public static String getCurrentKSTTime() {
String formattedTime;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// API 26 이상: java.time 사용
LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm");
formattedTime = now.format(formatter);
} else {
// API 26 미만: SimpleDateFormat 사용
SimpleDateFormat sdf = new SimpleDateFormat("HHmm", Locale.getDefault());
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Seoul"));
formattedTime = sdf.format(new Date());
}
return formattedTime;
}
}
Kotlin 버전
import android.os.Build
import java.text.SimpleDateFormat
import java.util.*
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
object TimeExample {
fun getCurrentKSTTime(): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// API 26 이상
val now = LocalDateTime.now(ZoneId.of("Asia/Seoul"))
val formatter = DateTimeFormatter.ofPattern("HHmm")
now.format(formatter)
} else {
// API 26 미만
val sdf = SimpleDateFormat("HHmm", Locale.getDefault())
sdf.timeZone = TimeZone.getTimeZone("Asia/Seoul")
sdf.format(Date())
}
}
}
결론
SimpleDateFormat 시대는 끝났습니다.
이제는 DateTimeFormatter + ZonedDateTime + ZoneId 조합으로 더 깔끔하고 안전하게 시간 데이터를 처리하세요
“KST 시간 변환, 이제 한 줄이면 충분합니다!”
반응형




