Java 版

Calendar 中设置好年、月(默认为当月),调用如下方法:

Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);  // 返回 int

Python 版

使用 calendar 模块中的 monthrange 方法

monthrange(year, month)
Returns weekday of first day of the month and number of days in month, for the specified year and month.

import calendar
calendar.monthrange(2017, 11)[1]  # 返回 30