ภาพรวม
ตัวอย่างนี้สาธิตการจัดรูปแบบวันที่และเวลา (Date/Time Formatting) ใน Android โดยใช้ SimpleDateFormat และ Calendar
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio เลือก File → New Project
ขั้นตอนที่ 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"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold|italic" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.kt (ใช้ SimpleDateFormat)
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView = findViewById<TextView>(R.id.textView)
val calendar = Calendar.getInstance()
// รูปแบบวันที่-เวลา: วัน, วันที่-เดือน-ปี ชั่วโมง-นาที-วินาที AM/PM
val simpleDateFormat = SimpleDateFormat(
"EEEE, dd-MMM-yyyy hh:mm:ss a",
Locale.getDefault()
)
val dateTime = simpleDateFormat.format(calendar.time)
textView.text = dateTime
}
}
รูปแบบวันที่ที่พบบ่อย (Common Patterns)
| Pattern | ผลลัพธ์ตัวอย่าง | คำอธิบาย |
|---|---|---|
yyyy-MM-dd | 2024-01-15 | วันที่มาตรฐาน ISO |
dd/MM/yyyy | 15/01/2024 | รูปแบบไทยทั่วไป |
EEEE, dd MMMM yyyy | Monday, 15 January 2024 | ชื่อวันและเดือนเต็ม |
HH:mm:ss | 14:30:45 | เวลา 24 ชั่วโมง |
hh:mm:ss a | 02:30:45 PM | เวลา 12 ชั่วโมงพร้อม AM/PM |
yyyy-MM-dd'T'HH:mm:ss | 2024-01-15T14:30:45 | รูปแบบ ISO 8601 |
วิธีการทำงานสมัยใหม่ (Java 8+ Time API)
ตั้งแต่ Android API 26+ (Android 8.0) ควรใช้ java.time package:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.ZoneId
// ใน onCreate
val now = LocalDateTime.now(ZoneId.systemDefault())
val formatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM yyyy HH:mm:ss", Locale.getDefault())
val formatted = now.format(formatter)
textView.text = formatted
ข้อดีของ java.time:
- Thread-safe (SimpleDateFormat ไม่ Thread-safe)
- Immutable objects
- API ที่ทันสมัยและอ่านง่ายกว่า
- รองรับ Time Zone ได้ดีกว่า
การทดสอบ
รันแอป จะแสดงวันที่และเวลาปัจจุบันในรูปแบบที่กำหนด เช่น "Monday, 15-Jan-2024 02:30:45 PM"