1:獲得該月最一天
public static String getFirstDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
// 設(shè)置月份
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
// 獲取某月最小天數(shù)
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
// 設(shè)置日歷中月份的最小天數(shù)
cal.set(Calendar.DAY_OF_MONTH, firstDay);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String firstDayOfMonth = sdf.format(cal.getTime()) + " 00:00:00";
return firstDayOfMonth;
}
2:獲得該月最后一天
/**
* 獲得該月最后一天
*
* @param year
* @param month
* @return
*/
public static String getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
// 設(shè)置月份
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
// 獲取某月最大天數(shù)
int lastDay = 0;
// 2月的平年瑞年天數(shù)
if (month == 2) {
lastDay = cal.getLeastMaximum(Calendar.DAY_OF_MONTH);
} else {
lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
// 設(shè)置日歷中月份的最大天數(shù)
cal.set(Calendar.DAY_OF_MONTH, lastDay);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime()) + " 23:59:59";
return lastDayOfMonth;
}