以下是使用 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 类,该类提供了方便的方法来计算年份和月份的总天数。

Java 代码计算指定月份天数 - 简单易懂

原文地址: https://www.cveoy.top/t/topic/ol9s 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录