java 根据年获取每个月最后一天

/**

* 获得该月最后一天

* @param month

* @param year

* @return

*/

public static String getLastDayOfMonth(int month,String year) {

Calendar cal = Calendar.getInstance();

SimpleDateFormat format =new SimpleDateFormat("yyyy");

String currentYear = format.format(cal.getTime());

// 获取某月最大天数

int lastDay =0;

if (!year.equals(currentYear)){

Date date = DateUtil.parseDateByYear(year);

cal.setTime(date);

}

cal.set(Calendar.MONTH, month -1);

//2月的平年瑞年天数

if(month==2) {

lastDay = cal.getLeastMaximum(Calendar.DAY_OF_MONTH);

}else {

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()).replaceAll("-","");

   return lastDayOfMonth;

}

你可能感兴趣的:(java 根据年获取每个月最后一天)