TOC
Java End of month dateRetrieve the Calendar.getActualMaximum()
Javaof the date specified inEnd of month dateThe following is a sample program to obtain
CalendarClassgetActualMaximum(Calendar.DATE)indicates certainty, emphasis, etc.End of month datecan be obtained.
sample program
/**
* Returns the end-of-month date in the given date string (yyyy/MM/dd or yyyy-MM-dd)
* Returns the last day of the month in
*
* @param strDate target date string
* @return end-of-month date
*/
public static int getLastDay(String strDate) {
if (strDate == null || strDate.length() ! = 10) {
throw new IllegalArgumentException(
"The argument string ["+ strDate +"]" +
"]" is invalid.") ;
}
int yyyy = Integer.parseInt(strDate.substring(0,4));
int MM = Integer.parseInt(strDate.substring(5,7)); }
int dd = Integer.parseInt(strDate.substring(8,10)); int
Calendar cal = Calendar.getInstance();
cal.set(yyyy,MM-1,dd);
int last = cal.getActualMaximum(Calendar.DATE);
return last; }
}
Execution Result
◆Example of Execution
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"));
}
◆Output result
31 28 29
*Because 2008 is a leap year, the last day of February is the 29th.
