Java给定年份和月份,返回这个月的天数/判断是否闰年

给定年份和月份,返回这个月的天数。

1≤year≤10000
1 \leq month \leq 121≤month≤12

【触碰到我的知识盲区了属于是//2月天数:平年有28天,闰年有29天。】

public class Solution {
    /**
     * @param year: a number year
     * @param month: a number month
     * @return: Given the year and the month, return the number of days of the month.
     */
    public int getTheMonthDays(int year, int month) {
        // write your code here
        int days;
        if(year<1||year>10000||month<1||month>12)
            return -1;
        else
        {
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                days=31;
                else if(month!=2)
                    days=30;
                    else 
                        if (year%400==0)
                            days=29;
                        else
                            if(year%4==0&&year%100!=0)
                                days=29;
                            else
                                days=28;
        }
            return days;
    }
}

判断给出的年份 n 是否为闰年. 如果 n 为闰年则返回 true

闰年是包含额外一天的日历年. 如果年份可以被 4 整除且不能被 100 整除 或者 可以被 400 整除, 那么这一年为闰年.

public class Solution {
    /**
     * @param n: a number represent year
     * @return: whether year n is a leap year.
     */
    public boolean isLeapYear(int n) 
    {
        // write your code here
        if (n%400==0)
             return true;
        else
            if(n%4==0&&n%100!=0)
                return true;
            else
                 return false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值