บทนำ: java.time.YearMonth
YearMonth แสดงปีและเดือนเท่านั้น (ไม่มีวัน) เหมาะสำหรับ: บิลรายเดือน, รายงานรายเดือน, Credit Card Expiry, การจองคิวรายเดือน
ตัวอย่าง: แสดงปีและเดือนปัจจุบัน
ขั้นตอนที่ 1-2: โปรเจกต์และ Layout
TextView ใน ConstraintLayout จัดกึ่งกลาง
ขั้นตอนที่ 3: MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
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.dateTextView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
YearMonth yearMonth = YearMonth.now();
// รูปแบบมาตรฐาน: 2024-01
String isoFormat = yearMonth.toString();
// รูปแบบไทย
DateTimeFormatter thaiFormatter = DateTimeFormatter
.ofPattern("yyyy 年 M 月", Locale.JAPANESE); // หรือใช้ THAI
// ไทย: ต้องใช้ Locale.THAI และ pattern ที่รองรับ
DateTimeFormatter thai = DateTimeFormatter
.ofPattern("yyyy 'ปี' M 'เดือน'", new Locale("th", "TH"));
String result = "ISO Format: " + isoFormat + "\n" +
"Thai Format: " + yearMonth.format(thai) + "\n" +
"Year: " + yearMonth.getYear() + "\n" +
"Month: " + yearMonth.getMonth() + "\n" +
"Month Value: " + yearMonth.getMonthValue() + "\n" +
"Days in Month: " + yearMonth.lengthOfMonth() + "\n" +
"Is Leap Year: " + yearMonth.isLeapYear() + "\n" +
"Previous Month: " + yearMonth.minusMonths(1) + "\n" +
"Next Month: " + yearMonth.plusMonths(1);
textView.setText(result);
} else {
textView.setText("ต้องการ Android 8.0 (API 26) ขึ้นไป");
}
}
}
YearMonth เมธอดสำคัญ
| เมธอด | คำอธิบาย | ตัวอย่าง |
|---|---|---|
now() | ปีเดือนปัจจุบัน | 2024-01 |
of(int year, int month) | สร้างจากปี/เดือน | YearMonth.of(2024, 1) |
getYear() | ปี | 2024 |
getMonth() | Month Enum | JANUARY |
getMonthValue() | เดือนเป็นตัวเลข 1-12 | 1 |
lengthOfMonth() | จำนวนวันในเดือน (28-31) | 31 |
isLeapYear() | ปีนั้นเป็นอธิกสุรทินหรือไม่ | true/false |
plusMonths(n) | บวก n เดือน | 2024-02 |
minusMonths(n) | ลบ n เดือน | 2023-12 |
atDay(int day) | แปลงเป็น LocalDate | LocalDate |
atEndOfMonth() | วันสุดท้ายของเดือน | LocalDate |
format(DateTimeFormatter) | จัดรูปแบบสตริง | String |
การจัดรูปแบบภาษาไทย
// ใช้ Locale.THAI (ต้องการ API 24+)
DateTimeFormatter thaiFormatter = DateTimeFormatter
.ofPattern("yyyy 'ปี' M 'เดือน'", Locale.forLanguageTag("th-TH"));
String thai = yearMonth.format(thaiFormatter); // "2567 ปี 1 เดือน" (พ.ศ.)
// หมายเหตุ: YearMonth ใช้ ค.ศ. ต้องแปลงเองหากต้องการ พ.ศ.
กรณีใช้งานจริง (Use Cases)
- Credit Card Expiry: MM/YY → YearMonth.parse("2025-12")
- Billing Cycle: เรียกเก็บเงินทุก 1 เดือน
- Subscription: ตรวจสอบหมดอายุรายเดือน
- Report/Analytics: จัดกลุ่มข้อมูลรายเดือน
- Calendar UI: แสดงปฏิทินรายเดือน
YearMonth vs LocalDate vs Year
| คลาส | องค์ประกอบ | เหมาะสำหรับ |
|---|---|---|
| Year | Year | รายงานรายปี, อายุ |
| YearMonth | Year + Month | Billing, Expiry, รายงานรายเดือน |
| LocalDate | Year + Month + Day | วันที่ทั่วไป, วันเกิด, นัดหมาย |
ผลลัพธ์
ISO Format: 2024-01 Thai Format: 2024 ปี 1 เดือน Year: 2024 Month: JANUARY Month Value: 1 Days in Month: 31 Is Leap Year: true Previous Month: 2023-12 Next Month: 2024-02