public class LastDayOfMonth
{
/**
* 獲取某月的最后一天
*
*/
public static String getLastDayOfMonth(int year,int month)
{
Calendar cal = Calendar.getInstance();
//設置年份
cal.set(Calendar.YEAR,year);
//設置月份
cal.set(Calendar.MONTH, month-1);
//獲取某月最大天數
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//設置日歷中月份的最大天數
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* main
*/
public static void main(String[] args)
{
String lastDay = getLastDayOfMonth(2014,5);
System.out.println("獲取當前月的最后一天:" + lastDay);
}
}