개요
이 글에서는 Kotlin을 사용하여 안드로이드에서 밀리초(밀리초 단위 타임스탬프)를 사람이 읽을 수 있는 날짜 형식으로 변환하는 방법을 단계별로 살펴봅니다. 밀리초 값은 1970년 1월 1일 00:00:00 UTC(에포크 시간)부터 경과한 시간을 의미하며, SimpleDateFormat 클래스를 활용하면 원하는 패턴의 날짜 문자열로 손쉽게 변환할 수 있습니다.
구현 단계
1단계 — 새 프로젝트 생성
Android Studio에서 File ⇒ New Project를 선택하고, 필요한 정보를 모두 입력하여 새 프로젝트를 생성합니다.
2단계 — 레이아웃 작성
res/layout/activity_main.xml 파일에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/holo_orange_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>3단계 — MainActivity 작성
src/MainActivity.kt 파일에 다음 코드를 추가합니다.
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.text.SimpleDateFormat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView: TextView = findViewById(R.id.textView)
val simpleDateFormat = SimpleDateFormat("dd/MM/yyyy")
val dateString = simpleDateFormat.format(9897546853323L)
textView.text = String.format("Date: %s", dateString)
}
}핵심 로직은 다음 두 줄입니다. 먼저 원하는 날짜 패턴으로 SimpleDateFormat 객체를 생성한 뒤, format() 메서드에 밀리초 값을 전달하면 지정된 패턴의 날짜 문자열이 반환됩니다.
val simpleDateFormat = SimpleDateFormat("dd/MM/yyyy")
val dateString = simpleDateFormat.format(9897546853323L)패턴은 필요에 따라 자유롭게 변경할 수 있습니다. 예를 들어 "yyyy-MM-dd HH:mm:ss"로 지정하면 시간까지 포함된 형식으로 출력됩니다.
4단계 — 매니페스트 설정
androidManifest.xml 파일에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>앱 실행 및 결과 확인
이제 애플리케이션을 실행해 결과를 확인해 보겠습니다. 실제 안드로이드 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run 아이콘을 클릭하세요. 실행 옵션 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 변환된 날짜가 표시됩니다.

참고: 더 현대적인 대안
SimpleDateFormat은 간편하지만 스레드 안전성(thread-safety) 문제가 있습니다. 따라서 API 26 이상을 지원하는 프로젝트라면 java.time 패키지의 Instant와 DateTimeFormatter를 사용하는 것이 권장됩니다. 예를 들어 다음과 같이 작성할 수 있습니다.
val instant = Instant.ofEpochMilli(9897546853323L)
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
.withZone(ZoneId.systemDefault())
val dateString = formatter.format(instant)이 방식은 불변(immutable) 객체를 사용하므로 멀티스레드 환경에서도 안전하게 동작합니다.