可以使用java.time.YearMonth类来实现这个方法。具体的实现如下:

import java.time.YearMonth;

public class MonthDaysCalculator {
    public static int getMonthDays(int year, int month) {
        YearMonth yearMonth = YearMonth.of(year, month);
        return yearMonth.lengthOfMonth();
    }

    public static void main(String[] args) {
        int year = 2022;
        int month = 2;
        int days = getMonthDays(year, month);
        System.out.println(year + "年" + month + "月有" + days + "天");
    }
}

在上面的代码中,getMonthDays方法接收年份和月份作为参数,然后使用YearMonth.of(year, month)创建一个YearMonth对象,再调用lengthOfMonth()方法获取该月份的天数。在main方法中,我们示范了如何使用该方法来获取指定年份和月份的天数,并打印输出结果。

请帮我写一个java方法传入一个年份和月份返回这个月有多少天

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

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