Giới thiệu
Ví dụ đơn giản minh họa cách chuyển đổi timestamp (mili-giây từ epoch) sang chuỗi ngày tháng định dạng dd/MM/yyyy sử dụng SimpleDateFormat trong Kotlin Android.
Các bước thực hiện
Bước 1: Tạo dự án mới
Android Studio > File > New Project.
Bước 2: 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>
Bước 3: MainActivity.kt
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.text.SimpleDateFormat
import java.util.Locale
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 sdf = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
val timestamp = 9897546853323L // Ví dụ timestamp
val dateString = sdf.format(timestamp)
textView.text = "Ngày: $dateString"
}
}
Bước 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.timestampconverter">
<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>
Chạy ứng dụng
Chạy app, TextView sẽ hiển thị: "Ngày: 17/03/2213" (tương ứng timestamp ví dụ).
Cách hiện đại khuyến nghị (Java 8+ / API 26+)
Nên sử dụng java.time package (có sẵn từ API 26, hoặc dùng library desugar cho API thấp hơn) thay vì SimpleDateFormat cũ:
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Locale
val instant = Instant.ofEpochMilli(timestamp)
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.getDefault())
.withZone(ZoneId.systemDefault())
val dateString = formatter.format(instant)
// Kết quả tương tự, nhưng thread-safe, immutable, API hiện đại
Các định dạng phổ biến
| Pattern | Ví dụ đầu ra | Mô tả |
|---|---|---|
| dd/MM/yyyy | 31/12/2023 | Ngày/Tháng/Năm |
| HH:mm:ss | 14:30:45 | Giờ:Phút:Giây (24h) |
| yyyy-MM-dd HH:mm | 2023-12-31 14:30 | ISO-like datetime |
| E, d MMM yyyy | Th 7, 31 Thg 12 2023 | Ngày trong tuần, locale-aware |