You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断LocalDateTime.now()或LocalDate.now()是否为当月最后一天并返回true

实现方案

重载方法支持两种入参类型

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

// 处理LocalDate入参
public static boolean isLastDayOfMonth(LocalDate date) {
    return date.equals(date.with(TemporalAdjusters.lastDayOfMonth()));
}

// 处理LocalDateTime入参,转换为LocalDate后复用上述逻辑
public static boolean isLastDayOfMonth(LocalDateTime dateTime) {
    return isLastDayOfMonth(dateTime.toLocalDate());
}

调用方式

// 校验当前日期是否为当月最后一天
boolean checkByLocalDate = isLastDayOfMonth(LocalDate.now());
boolean checkByLocalDateTime = isLastDayOfMonth(LocalDateTime.now());

无参快捷方法(仅校验当前系统时间)

如果不需要传入指定时间,只需要判断当前系统时间是否为当月最后一天,可以直接封装无参方法:

public static boolean isCurrentDayLastOfMonth() {
    LocalDate now = LocalDate.now();
    return now.equals(now.with(TemporalAdjusters.lastDayOfMonth()));
}

内容的提问来源于stack exchange,提问作者None

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 13:54:04