java获得某个月的天数

时间:2023-12-04 15:35:20

/**
* 方法一
*/

String strDate = "2012-02";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Date date = format.parse(strDate);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int days1 = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("天数为=" + days1);

/**
* 方法二
*/
int days2 = new Date(2012,02,0).getDate();
System.out.println("天数为=" + days2);