Class YearMonthDayInterval

  • All Implemented Interfaces:
    Serializable

    public final class YearMonthDayInterval
    extends Object
    implements Serializable
    An immutable date interval where both the start and end are inclusive, unless stated otherwise. Start and end YearMonthDay instances cannot be null and start date must be before or the same date as the end date.
    Since:
    1.3.0
    Author:
    Pascal Essiembre
    See Also:
    Serialized Form
    • Constructor Detail

      • YearMonthDayInterval

        public YearMonthDayInterval​(String interval)
        Constructs a YearMonthDayInterval out of a string. The recommended string format is yyyy-MM-dd - yyyy-MM-dd, but any characters in between the start and end are accepted as long as there is a space after the start YearMontDay and before the end YearMonthDay.
        Parameters:
        interval - the interval to parse
      • YearMonthDayInterval

        public YearMonthDayInterval​(Date start,
                                    Date end)
      • YearMonthDayInterval

        public YearMonthDayInterval​(Calendar start,
                                    Calendar end)
    • Method Detail

      • getStartDate

        public Date getStartDate()
      • getEndDate

        public Date getEndDate()
      • getEndDateEndOfDay

        public Date getEndDateEndOfDay()
        Gets the end date as midnight the day after to ensure all dates on the same day as this YearMonthDay are smaller than this returned exclusive date. Useful for date range comparisons where typically the end date is exclusive.
        Returns:
        midnight past the end date
      • contains

        public boolean contains​(YearMonthDay ymd)
        Whether the YearMonthDay falls between this interval (inclusive endpoints).
        Parameters:
        ymd - the YearMonthDay
        Returns:
        true if YearMonthDay is included in this interval
      • contains

        public boolean contains​(Date date)
        Whether the date falls between this interval (inclusive endpoints).
        Parameters:
        date - a date
        Returns:
        true if the date is included in this interval
      • getYears

        public int getYears()
        Gets the number of years between start and end dates, rounded down.
        Returns:
        number of years
      • getMonths

        public int getMonths()
        Gets the number of months between start and end dates, rounded down.
        Returns:
        number of months
      • getDays

        public int getDays()
        Gets the number of days between start and end dates, rounded down.
        Returns:
        number of days
      • toString

        public String toString()
        Gets the interval as a string of this format: yyyy-MM-dd - yyyy-MM-dd;
        Overrides:
        toString in class Object
        Returns:
        interval as string