विषयसूची
जावा महीने के अंत की तारीखपाना Calendar.getActualMaximum()
जावानिर्दिष्ट तिथिमहीने के अंत की तारीखयह डेटा प्राप्त करने के लिए एक नमूना प्रोग्राम है।
जावा.यूटिल.कैलेंडरकक्षा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 तारीख था।
