代码实现如下:
import java.util.Calendar; public class GetDay { public static void main(String[] args) { int currentMaxDays = getCurrentMonthDay(); int maxDaysByDate = getDaysByYearMonth(2017, 9); System.out.println("本月天数:" + currentMaxDays); System.out.println("2017年9月天数:" + maxDaysByDate); } /** * 获取当月的 天数 */ public static int getCurrentMonthDay() { Calendar a = Calendar.getInstance(); a.set(Calendar.DATE, 1); a.roll(Calendar.DATE, -1); int maxDate = a.get(Calendar.DATE); return maxDate; } /** * 根据年 月 获取对应的月份 天数 */ public static int getDaysByYearMonth(int year, int month) { Calendar a = Calendar.getInstance(); a.set(Calendar.YEAR, year); a.set(Calendar.MONTH, month - 1); a.set(Calendar.DATE, 1); a.roll(Calendar.DATE, -1); int maxDate = a.get(Calendar.DATE); return maxDate; } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/14588.html