Class DatePredicates

java.lang.Object
cloud.opencode.base.date.DatePredicates

public final class DatePredicates extends Object
Static Predicate Methods for Date/Time Objects 日期时间对象的静态谓词方法

This utility class provides a comprehensive set of predicate methods for testing properties of date and time objects. All methods are null-safe and return false when any argument is null.

此工具类提供一套完整的谓词方法,用于测试日期时间对象的属性。 所有方法均为空值安全,当任何参数为 null 时返回 false

Features | 主要功能:

  • Future/Past checks - 未来/过去检查
  • Same day/month/year/week comparisons - 同日/同月/同年/同周比较
  • Day-of-month position checks (first/last) - 月中日位置检查(首日/末日)
  • Day-of-week checks (Monday through Sunday, weekend/weekday) - 星期几检查
  • Leap year detection - 闰年检测
  • Range inclusion (between) checks - 范围包含检查

Usage Examples | 使用示例:

LocalDate today = LocalDate.now();
LocalDate christmas = LocalDate.of(2026, 12, 25);

boolean future = DatePredicates.isFuture(christmas);
boolean weekend = DatePredicates.isWeekend(today);
boolean sameMonth = DatePredicates.isSameMonth(today, christmas);
boolean between = DatePredicates.isBetween(today,
    LocalDate.of(2026, 1, 1), LocalDate.of(2026, 12, 31));

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-date V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isFuture

      public static boolean isFuture(LocalDate date)
      Checks if the given date is strictly after today. 检查给定日期是否严格在今天之后。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is in the future | 如果日期在未来则返回 true
    • isFuture

      public static boolean isFuture(LocalDateTime dateTime)
      Checks if the given date-time is strictly after the current date-time. 检查给定日期时间是否严格在当前日期时间之后。
      Parameters:
      dateTime - the date-time to check | 要检查的日期时间
      Returns:
      true if the date-time is in the future | 如果日期时间在未来则返回 true
    • isPast

      public static boolean isPast(LocalDate date)
      Checks if the given date is strictly before today. 检查给定日期是否严格在今天之前。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is in the past | 如果日期在过去则返回 true
    • isPast

      public static boolean isPast(LocalDateTime dateTime)
      Checks if the given date-time is strictly before the current date-time. 检查给定日期时间是否严格在当前日期时间之前。
      Parameters:
      dateTime - the date-time to check | 要检查的日期时间
      Returns:
      true if the date-time is in the past | 如果日期时间在过去则返回 true
    • isSameDay

      public static boolean isSameDay(LocalDate a, LocalDate b)
      Checks if two dates represent the same day. 检查两个日期是否表示同一天。
      Parameters:
      a - the first date | 第一个日期
      b - the second date | 第二个日期
      Returns:
      true if both dates are the same day | 如果两个日期是同一天则返回 true
    • isSameDay

      public static boolean isSameDay(LocalDateTime a, LocalDateTime b)
      Checks if two date-times represent the same day (ignoring time). 检查两个日期时间是否表示同一天(忽略时间部分)。
      Parameters:
      a - the first date-time | 第一个日期时间
      b - the second date-time | 第二个日期时间
      Returns:
      true if both date-times fall on the same day | 如果两个日期时间在同一天则返回 true
    • isSameMonth

      public static boolean isSameMonth(LocalDate a, LocalDate b)
      Checks if two dates fall in the same year and month. 检查两个日期是否在同一年同一月。
      Parameters:
      a - the first date | 第一个日期
      b - the second date | 第二个日期
      Returns:
      true if both dates share the same year and month | 如果两个日期年月相同则返回 true
    • isSameYear

      public static boolean isSameYear(LocalDate a, LocalDate b)
      Checks if two dates fall in the same year. 检查两个日期是否在同一年。
      Parameters:
      a - the first date | 第一个日期
      b - the second date | 第二个日期
      Returns:
      true if both dates share the same year | 如果两个日期年份相同则返回 true
    • isSameWeek

      public static boolean isSameWeek(LocalDate a, LocalDate b)
      Checks if two dates fall in the same ISO week (same week-based year and week number). 检查两个日期是否在同一 ISO 周(相同的基于周的年份和周数)。
      Parameters:
      a - the first date | 第一个日期
      b - the second date | 第二个日期
      Returns:
      true if both dates are in the same ISO week | 如果两个日期在同一 ISO 周则返回 true
    • isFirstDayOfMonth

      public static boolean isFirstDayOfMonth(LocalDate date)
      Checks if the given date is the first day of its month. 检查给定日期是否为所在月份的第一天。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is the first day of the month | 如果日期是月份第一天则返回 true
    • isLastDayOfMonth

      public static boolean isLastDayOfMonth(LocalDate date)
      Checks if the given date is the last day of its month. 检查给定日期是否为所在月份的最后一天。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is the last day of the month | 如果日期是月份最后一天则返回 true
    • isMonday

      public static boolean isMonday(LocalDate date)
      Checks if the given date is a Monday. 检查给定日期是否为周一。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is Monday | 如果日期为周一则返回 true
    • isTuesday

      public static boolean isTuesday(LocalDate date)
      Checks if the given date is a Tuesday. 检查给定日期是否为周二。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is Tuesday | 如果日期为周二则返回 true
    • isWednesday

      public static boolean isWednesday(LocalDate date)
      Checks if the given date is a Wednesday. 检查给定日期是否为周三。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is Wednesday | 如果日期为周三则返回 true
    • isThursday

      public static boolean isThursday(LocalDate date)
      Checks if the given date is a Thursday. 检查给定日期是否为周四。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is Thursday | 如果日期为周四则返回 true
    • isFriday

      public static boolean isFriday(LocalDate date)
      Checks if the given date is a Friday. 检查给定日期是否为周五。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is Friday | 如果日期为周五则返回 true
    • isSaturday

      public static boolean isSaturday(LocalDate date)
      Checks if the given date is a Saturday. 检查给定日期是否为周六。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is Saturday | 如果日期为周六则返回 true
    • isSunday

      public static boolean isSunday(LocalDate date)
      Checks if the given date is a Sunday. 检查给定日期是否为周日。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is Sunday | 如果日期为周日则返回 true
    • isWeekend

      public static boolean isWeekend(LocalDate date)
      Checks if the given date falls on a weekend (Saturday or Sunday). 检查给定日期是否为周末(周六或周日)。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is Saturday or Sunday | 如果日期为周六或周日则返回 true
    • isWeekday

      public static boolean isWeekday(LocalDate date)
      Checks if the given date falls on a weekday (Monday through Friday). 检查给定日期是否为工作日(周一至周五)。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the date is a weekday | 如果日期为工作日则返回 true
    • isLeapYear

      public static boolean isLeapYear(LocalDate date)
      Checks if the given date falls in a leap year. 检查给定日期所在年份是否为闰年。
      Parameters:
      date - the date to check | 要检查的日期
      Returns:
      true if the year is a leap year | 如果年份为闰年则返回 true
    • isBetween

      public static boolean isBetween(LocalDate date, LocalDate start, LocalDate end)
      Checks if the given date is between start and end (inclusive on both ends). 检查给定日期是否在起始和结束之间(两端都包含)。

      Note: the range is not validated. If start is after end (an inverted range), this method silently returns false for every input — it does not throw.

      注意:不校验区间方向。若 start 晚于 end(倒置区间), 本方法对任何输入都静默返回 false,不抛异常。

      Parameters:
      date - the date to check | 要检查的日期
      start - the start of the range (inclusive) | 范围起始(包含)
      end - the end of the range (inclusive) | 范围结束(包含)
      Returns:
      true if the date is within the range | 如果日期在范围内则返回 true
    • isBetween

      public static boolean isBetween(LocalDateTime dateTime, LocalDateTime start, LocalDateTime end)
      Checks if the given date-time is between start and end (inclusive on both ends). 检查给定日期时间是否在起始和结束之间(两端都包含)。

      Note: the range is not validated. If start is after end (an inverted range), this method silently returns false for every input — it does not throw.

      注意:不校验区间方向。若 start 晚于 end(倒置区间), 本方法对任何输入都静默返回 false,不抛异常。

      Parameters:
      dateTime - the date-time to check | 要检查的日期时间
      start - the start of the range (inclusive) | 范围起始(包含)
      end - the end of the range (inclusive) | 范围结束(包含)
      Returns:
      true if the date-time is within the range | 如果日期时间在范围内则返回 true