Java 日期工具:计算指定月份的天数
public class MonthDaysCalculator {\n\n public static int getDaysInMonth(int year, int month) {\n if (month < 1 || month > 12) {\n throw new IllegalArgumentException("Invalid month!");\n }\n \n int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n \n if (month == 2 && isLeapYear(year)) {\n return 29;\n }\n \n return daysInMonth[month - 1];\n }\n \n private static boolean isLeapYear(int year) {\n if (year % 4 != 0) {\n return false;\n } else if (year % 100 != 0) {\n return true;\n } else if (year % 400 != 0) {\n return false;\n } else {\n return true;\n }\n }\n}
原文地址: https://www.cveoy.top/t/topic/p9Xy 著作权归作者所有。请勿转载和采集!