public class TimeUtil { private static final WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1); /** * 以second为主进行判断 * first是开始时间 second结束时间 * * @param first * @param second * @return */ public static Boolean checkSameWeek(LocalDate first, LocalDate second) { if (first.isAfter(second)) { throw new IllegalArgumentException(""); } if (first.getYear() == second.getYear()) { return first.get(weekFields.weekOfWeekBasedYear()) == second.get(weekFields.weekOfWeekBasedYear()); } int day = first.getDayOfWeek().getValue(); LocalDate plus = first.plusDays(7 - day); return second.toEpochDay() <= plus.toEpochDay(); } }
java 判断 两个日期是否在同一个星期内
最新推荐文章于 2022-03-12 09:19:27 发布
该代码段展示了一个Java方法,用于检查两个LocalDate对象是否属于同一周。方法首先确保开始日期不晚于结束日期,然后通过比较它们在基于Monday的第一天的weekOfWeekBasedYear字段来判断。如果年份相同,则直接比较此字段;否则,通过计算并比较它们转换为EpochDay后的值来判断。
摘要由CSDN通过智能技术生成