如何判断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
相关产品推荐
相关产品推荐

