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

วิธีดึงเวลาและวันที่ปัจจุบันบน Android

ภาพรวม

บทความนี้สาธิตวิธีการแสดงวันที่และเวลาปัจจุบันในแอปพลิเคชัน Android โดยใช้คลาส SimpleDateFormat จาก Java ซึ่งช่วยจัดรูปแบบวันที่และเวลาได้อย่างยืดหยุ่นตาม Locale ที่ต้องการ

ขั้นตอนการสร้างโปรเจกต์

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio → File → New Project → เลือก Empty Activity → กรอกข้อมูลที่จำเป็น → Finish

ขั้นตอนที่ 2: จัดรูปแบบ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:layout_marginBottom="36dp" />
</RelativeLayout>

คำอธิบาย: ใช้ TextView เพื่อแสดงผลวันที่และเวลา จัดตำแหน่งกึ่งกลางแนวนอน ขนาดตัวอักษร 30sp

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.date);
        
        // จัดรูปแบบวันที่: ปี.เดือน.วัน G 'at' ชั่วโมง:นาที:วินาที โซนเวลา
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z", Locale.getDefault());
        String currentDateTime = sdf.format(new Date());
        
        textView.setText(currentDateTime);
    }
}

จุดสำคัญ:

  • ใช้ Locale.getDefault() เพื่อรองรับภาษาและภูมิภาคของผู้ใช้
  • รูปแบบ yyyy.MM.dd G 'at' HH:mm:ss z แสดงผลเช่น 2024.01.15 AD at 14:30:45 ICT
  • สามารถปรับรูปแบบได้ตามต้องการ เช่น dd/MM/yyyy HH:mm:ss

ขั้นตอนที่ 4: ไฟล์ Manifest

ไม่ต้องแก้ไข AndroidManifest.xml

การทดสอบ

รันแอปบนอุปกรณ์จริงหรือ Emulator จะเห็นวันที่และเวลาปัจจุบันแสดงบนหน้าจอ

รูปแบบวันที่ที่ใช้บ่อย

รูปแบบผลลัพธ์ตัวอย่าง
dd/MM/yyyy15/01/2024
yyyy-MM-dd HH:mm:ss2024-01-15 14:30:45
EEE, d MMM yyyyMon, 15 Jan 2024
hh:mm a02:30 PM

หมายเหตุ: ตั้งแต่ API 26 (Android 8.0) ขึ้นไป แนะนำให้ใช้ java.time package แทน SimpleDateFormat เพื่อประสิทธิภาพและความปลอดภัยด้านเธรดที่ดีกว่า

ภาพรวม

บทความนี้สาธิตวิธีการแสดงวันที่และเวลาปัจจุบันในแอปพลิเคชัน Android โดยใช้คลาส SimpleDateFormat จาก Java ซึ่งช่วยจัดรูปแบบวันที่และเวลาได้อย่างยืดหยุ่นตาม Locale ที่ต้องการ

ขั้นตอนการสร้างโปรเจกต์

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio → File → New Project → เลือก Empty Activity → กรอกข้อมูลที่จำเป็น → Finish

ขั้นตอนที่ 2: จัดรูปแบบ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:layout_marginBottom="36dp" />
</RelativeLayout>

คำอธิบาย: ใช้ TextView เพื่อแสดงผลวันที่และเวลา จัดตำแหน่งกึ่งกลางแนวนอน ขนาดตัวอักษร 30sp

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.date);
        
        // จัดรูปแบบวันที่: ปี.เดือน.วัน G 'at' ชั่วโมง:นาที:วินาที โซนเวลา
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z", Locale.getDefault());
        String currentDateTime = sdf.format(new Date());
        
        textView.setText(currentDateTime);
    }
}

จุดสำคัญ:

  • ใช้ Locale.getDefault() เพื่อรองรับภาษาและภูมิภาคของผู้ใช้
  • รูปแบบ yyyy.MM.dd G 'at' HH:mm:ss z แสดงผลเช่น 2024.01.15 AD at 14:30:45 ICT
  • สามารถปรับรูปแบบได้ตามต้องการ เช่น dd/MM/yyyy HH:mm:ss

ขั้นตอนที่ 4: ไฟล์ Manifest

ไม่ต้องแก้ไข AndroidManifest.xml

การทดสอบ

รันแอปบนอุปกรณ์จริงหรือ Emulator จะเห็นวันที่และเวลาปัจจุบันแสดงบนหน้าจอ

รูปแบบวันที่ที่ใช้บ่อย

รูปแบบผลลัพธ์ตัวอย่าง
dd/MM/yyyy15/01/2024
yyyy-MM-dd HH:mm:ss2024-01-15 14:30:45
EEE, d MMM yyyyMon, 15 Jan 2024
hh:mm a02:30 PM

หมายเหตุ: ตั้งแต่ API 26 (Android 8.0) ขึ้นไป แนะนำให้ใช้ java.time package แทน SimpleDateFormat เพื่อประสิทธิภาพและความปลอดภัยด้านเธรดที่ดีกว่า