Record Class DateRange

java.lang.Object
java.lang.Record
nl.colorize.util.stats.DateRange
All Implemented Interfaces:
Comparable<DateRange>, Predicate<Date>

public record DateRange(Date start, Date end) extends Record implements Predicate<Date>, Comparable<DateRange>
Defines a range between two Dates, with the start date being inclusive and the end date being exclusive. Although the name of this class implies a date range, this is technically a date/time range since Date instances always include a time.

If dates do not specify an explicit time zone, the default time zone will be used. See Platform.getDefaultTimeZone() for how to configure the default time zone.

  • Constructor Details

    • DateRange

      public DateRange(Date start, Date end)
      Creates an instance of a DateRange record class.
      Parameters:
      start - the value for the start record component
      end - the value for the end record component
    • DateRange

      public DateRange(String start, String end)
      Secondary constructor that uses DateParser.parse(String) to parse dates from strings.
  • Method Details

    • contains

      public boolean contains(Date date)
      Returns true if this date range contains the specified date. Note the start date is considered inclusive but the end date is considered exclusive, so this method will return false if the argument matches the end date exactly.
    • test

      public boolean test(Date date)
      Returns true if this date range includes the specified date, using the same logic describes in contains(Date).
      Specified by:
      test in interface Predicate<Date>
    • splitDays

      public List<DateRange> splitDays()
      Splits this date range into daily intervals. This might include partial days, depending on the start and end date of this date range.
    • splitWeeks

      public List<DateRange> splitWeeks()
      Splits this date range into weekly intervals. This might include partial weeks, depending on the start and end date of this date range.
    • splitMonths

      public List<DateRange> splitMonths()
      Splits this date range into monthly intervals. This might include partial months, depending on the start and end date of this date range.
    • splitYearly

      public List<DateRange> splitYearly()
      Splits this date range into yearly intervals. This might include partial years, depending on the start and end date of this date range.
    • span

      public DateRange span(DateRange other)
      Creates a new date range that represents the smallest possible period that includes both this date range and the specified other date range.
    • compareTo

      public int compareTo(DateRange other)
      Specified by:
      compareTo in interface Comparable<DateRange>
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • start

      public Date start()
      Returns the value of the start record component.
      Returns:
      the value of the start record component
    • end

      public Date end()
      Returns the value of the end record component.
      Returns:
      the value of the end record component