สารบัญ
ชวา วันที่สิ้นเดือนรับ ปฏิทิน.getActualMaximum()
ชวาวันที่ระบุวันที่สิ้นเดือนนี่คือโปรแกรมตัวอย่างเพื่อดึง [ข้อมูล]
java.util.CalendarระดับgetActualMaximum(Calendar.DATE)でวันที่สิ้นเดือนคุณสามารถหาซื้อได้
ตัวอย่างโปรแกรม
/**
* 指定した日付文字列(yyyy/MM/dd or yyyy-MM-dd)
* における月末日付を返します。
*
* @param strDate 対象の日付文字列
* @return 月末日付
*/
public static int getLastDay(String strDate) {
if (strDate == null || strDate.length() != 10) {
throw new IllegalArgumentException(
"引数の文字列["+ strDate +"]" +
"は不正です。");
}
int yyyy = Integer.parseInt(strDate.substring(0,4));
int MM = Integer.parseInt(strDate.substring(5,7));
int dd = Integer.parseInt(strDate.substring(8,10));
Calendar cal = Calendar.getInstance();
cal.set(yyyy,MM-1,dd);
int last = cal.getActualMaximum(Calendar.DATE);
return last;
}
ผลลัพธ์การดำเนินการ
◆ตัวอย่าง
public static void main(String[] args) { System.out.println(getLastDay("2007/01/01")); System.out.println(getLastDay("2007/02/01")); System.out.println(getLastDay("2008/02/01")); }
◆ผลลัพธ์
31 28 29
*เนื่องจากปี 2008 เป็นปีอธิกสุรทิน วันสุดท้ายของเดือนกุมภาพันธ์จึงตรงกับวันที่ 29
