JAVA8日期API获取当前月份最大日数--Api不确定

做整年的日期的数据的按月归档及数据结构的用到当前月份的最大日子,so 代码如下

LocalDate date = LocalDate.parse(calDay, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
int maxDay=date.getMonth().maxLength();

这串JAVA8日期处理用完觉得很润是不是,But在接下来调试中发现在2022-02月的日子里这样获取返回值竟然是29,29天,有点不对劲。查了windows的日历表,2022-02月份的日子数一共是28天才对。
在这里插入图片描述
查查我生成的数据是没问题的,因此直接debug喽。
在这里插入图片描述
真的给我搞个29,太错误了吧,跟进api看源码


    /**
     * Gets the maximum length of this month in days.
     * <p>
     * February has a maximum length of 29 days.
     * April, June, September and November have 30 days.
     * All other months have 31 days.
     *
     * @return the maximum length of this month in days, from 29 to 31
     */
    public int maxLength() {
        switch (this) {
            case FEBRUARY:
                return 29;
            case APRIL:
            case JUNE:
            case SEPTEMBER:
            case NOVEMBER:
                return 30;
            default:
                return 31;
        }
    }

怎么看也不对劲,凑巧上边还有个方法

 /**
     * Gets the minimum length of this month in days.
     * <p>
     * February has a minimum length of 28 days.
     * April, June, September and November have 30 days.
     * All other months have 31 days.
     *
     * @return the minimum length of this month in days, from 28 to 31
     */
    public int minLength() {
        switch (this) {
            case FEBRUARY:
                return 28;
            case APRIL:
            case JUNE:
            case SEPTEMBER:
            case NOVEMBER:
                return 30;
            default:
                return 31;
        }
    }

感觉有点像Calenda类当时的minLength()其实是实际的日子数吧,但我还是不确定直接用这api闰年怎么搞,不能我还判断年份吧,超麻烦了,直接判断日期算了,代码如下:

date.with(TemporalAdjusters.lastDayOfMonth())

直接判断日期是否是本月最后一天即可。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值