@[TOC]LocalDate比较大小
项目场景:
LocalDate比较大小,遇到这个问题搜索到的高浏览答案结果竟然是反的,自己记录一下
解决方案:
源码:
default boolean isAfter(ChronoLocalDate other) {
return this.toEpochDay() > other.toEpochDay();
}
自己代码:
if (contractEntity.getStartTime().isAfter(contractEntity.getExpireTime())) {
throw new CommonException("开始日期需小于终止日期!");
}
测试:
//getStartTime():2021-12-25 getExpireTime():2021-12-30
if (contractEntity.getStartTime().isAfter(contractEntity.getExpireTime())) {//false
throw new CommonException("开始日期需小于终止日期!");
}
if (contractEntity.getStartTime().isBefore(contractEntity.getExpireTime())) {//true
throw new CommonException("开始日期需小于终止日期!");
}