java计算某月总天数代码
以下是Java代码,用于计算给定月份的总天数:
import java.time.YearMonth;
public class MonthDaysCalculator {
public static void main(String args[]) {
int year = 2021; // 年份
int month = 2; // 月份,2代表二月份
// 计算给定月份的总天数
YearMonth yearMonth = YearMonth.of(year, month);
int daysInMonth = yearMonth.lengthOfMonth();
System.out.println("The number of days in month " + month + " of year " + year + " is " + daysInMonth);
}
}
输出结果:
The number of days in month 2 of year 2021 is 28
请注意,以上代码使用了Java 8中的YearMonth类,该类提供了方便的方法来计算年份和月份的总天数
原文地址: https://www.cveoy.top/t/topic/fGod 著作权归作者所有。请勿转载和采集!