Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีจัดรูปแบบวันที่และเวลาใน Android ด้วย Kotlin

ภาพรวม

ตัวอย่างนี้สาธิตการจัดรูปแบบวันที่และเวลา (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-dd2024-01-15วันที่มาตรฐาน ISO
dd/MM/yyyy15/01/2024รูปแบบไทยทั่วไป
EEEE, dd MMMM yyyyMonday, 15 January 2024ชื่อวันและเดือนเต็ม
HH:mm:ss14:30:45เวลา 24 ชั่วโมง
hh:mm:ss a02:30:45 PMเวลา 12 ชั่วโมงพร้อม AM/PM
yyyy-MM-dd'T'HH:mm:ss2024-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"