输入年份和月份,输出这一年的这一月有多少天。需要考虑闰年。

本文展示了如何在Java中利用YearMonth类获取给定年份和月份的天数,通过`YearMonth.of(year,month)`创建对象,然后调用`lengthOfMonth()`方法获取相应月份的天数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.luogu.com.cn/problem/P5716icon-default.png?t=N7T8https://www.luogu.com.cn/problem/P5716


import java.time.YearMonth;
import java.util.*;

public class Main{
    public static void main (String[] args){
        Scanner sc =new Scanner(System.in);
        int y =sc.nextInt();
        int m =sc.nextInt();

        YearMonth ym=YearMonth.of(y, m);
        int days=ym.lengthOfMonth();
        System.out.print(days);
        
    }
}
        

import java.time.YearMonth;

public class Main {
    public static void main(String[] args) {
        int year = 2024; // 你想要查询的年份
        int month = 3; // 你想要查询的月份

        YearMonth yearMonth = YearMonth.of(year, month);
        int daysInMonth = yearMonth.lengthOfMonth();

        System.out.println("Year: " + year + ", Month: " + month + ", Days: " + daysInMonth);
    }
}

1. `YearMonth.of(year, month);` - 这行代码使用指定的年份和月份创建了一个 `YearMonth` 对象。`YearMonth` 类表示了一个特定的年份和月份,它不包含具体的日期,只包含了年份和月份信息。

2. `yearMonth.lengthOfMonth();` - 这行代码调用了 `YearMonth` 对象的 `lengthOfMonth()` 方法,该方法返回了指定年份和月份对应的天数。因为不同月份的天数是不同的,所以这个方法会返回特定月份的天数。

通过这两行代码,你可以获取到指定年份和月份的天数。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值