Class CalendarPeriod<T>

java.lang.Object
net.time4j.range.CalendarPeriod<T>
Type Parameters:
T - generic type of incomplete calendar date (without day part) or dates in other calendar systems
All Implemented Interfaces:
Serializable, ChronoInterval<T>

public class CalendarPeriod<T> extends Object implements ChronoInterval<T>, Serializable

Represents a closed interval between two incomplete calendar dates like months, weeks, quarters or years.

This class can also represent generic intervals using different calendar systems.

Since:
5.0
Author:
Meno Hochschild
See Also:
Serialized Form
  • Method Details

    • between

      public static CalendarPeriod<CalendarYear> between(CalendarYear y1, CalendarYear y2)
      Creates a calendar period for given year range.
      Parameters:
      y1 - first calendar year
      y2 - last calendar year (inclusive)
      Returns:
      CalendarPeriod
    • between

      public static CalendarPeriod<CalendarQuarter> between(CalendarQuarter q1, CalendarQuarter q2)
      Creates a calendar period for given quarter year range.
      Parameters:
      q1 - first quarter year
      q2 - last quarter year (inclusive)
      Returns:
      CalendarPeriod
    • between

      public static CalendarPeriod<CalendarMonth> between(CalendarMonth m1, CalendarMonth m2)
      Creates a calendar period for given month range.
      Parameters:
      m1 - first calendar month
      m2 - last calendar month (inclusive)
      Returns:
      CalendarPeriod
    • between

      public static CalendarPeriod<CalendarWeek> between(CalendarWeek w1, CalendarWeek w2)
      Creates a calendar period for given calendar week range.
      Parameters:
      w1 - first calendar week
      w2 - last calendar week (inclusive)
      Returns:
      CalendarPeriod
    • onYears

      public static CalendarPeriod.Parser<CalendarYear> onYears()

      Defines a parser on which new calendar intervals for years can be created.

      Returns:
      new interval factory intended for parsing
    • onQuarters

      public static CalendarPeriod.Parser<CalendarQuarter> onQuarters()

      Defines a parser on which new calendar intervals for quarters can be created.

      Returns:
      new interval factory intended for parsing
    • onMonths

      public static CalendarPeriod.Parser<CalendarMonth> onMonths()

      Defines a parser on which new calendar intervals for months can be created.

      Returns:
      new interval factory intended for parsing
    • onWeeks

      public static CalendarPeriod.Parser<CalendarWeek> onWeeks()

      Defines a parser on which new calendar intervals for weeks can be created.

      Important: Use the root locale to make sure that the parser will use the ISO-8601-definition of a calendar week. Example:

           CalendarPeriod<CalendarWeek> expected =
             CalendarPeriod.between(
               CalendarWeek.of(2017, 52),
               CalendarWeek.of(2020, 4));
           ChronoFormatter<CalendarWeek> f =
             ChronoFormatter.ofPattern( // use root locale for getting the week of year as ISO-week
               "w. 'week of' YYYY", PatternType.CLDR, Locale.ROOT, CalendarWeek.chronology());
           assertThat(CalendarPeriod.onWeeks().parse("52. week of 2017 – 4. week of 2020", f), is(expected));
       
      Returns:
      new interval factory intended for parsing
    • on

      public static <U,​ D extends Calendrical<U,​ D>> CalendarPeriod.Factory<D> on(TimeAxis<U,​D> axis)

      Defines a timeline on which new generic calendar intervals can be created.

      Example:

               PersianCalendar start = PersianCalendar.of(1392, PersianMonth.ESFAND, 27);
               PersianCalendar end = PersianCalendar.of(1393, PersianMonth.FARVARDIN, 6);
      
               CalendarPeriod<PersianCalendar> i1 =
                 CalendarPeriod.on(PersianCalendar.axis()).between(start, end);
               CalendarPeriod<PersianCalendar> i2 =
                 CalendarPeriod.on(PersianCalendar.axis()).between(
                   end.minus(CalendarDays.ONE),
                   end.plus(CalendarDays.ONE));
      
               System.out.println(
                 interval.findIntersection(
                   CalendarPeriod.on(PersianCalendar.axis()).between(
                     end.minus(CalendarDays.ONE), end.plus(CalendarDays.ONE))).get());
               // [AP-1393-01-05/AP-1393-01-06]
       
      Type Parameters:
      U - generic unit type
      D - generic type of timepoints on the underlying timeline
      Parameters:
      axis - the calendrical timeline
      Returns:
      new interval factory
    • on

      public static <D extends CalendarVariant<D>> CalendarPeriod.Factory<D> on(CalendarFamily<D> family, String variant)

      Defines a timeline on which new generic calendar intervals can be created.

      Type Parameters:
      D - generic type of timepoints on the underlying timeline
      Parameters:
      family - calendar family
      variant - calendar variant
      Returns:
      new interval factory
      See Also:
      on(CalendarFamily, VariantSource)
    • on

      public static <D extends CalendarVariant<D>> CalendarPeriod.Factory<D> on(CalendarFamily<D> family, VariantSource variant)

      Defines a timeline on which new generic calendar intervals can be created.

      Type Parameters:
      D - generic type of timepoints on the underlying timeline
      Parameters:
      family - calendar family
      variant - calendar variant
      Returns:
      new interval factory
      See Also:
      on(CalendarFamily, String)
    • getStart

      public Boundary<T> getStart()
      Description copied from interface: ChronoInterval

      Yields the lower bound of this interval.

      Specified by:
      getStart in interface ChronoInterval<T>
      Returns:
      start interval boundary
    • getEnd

      public Boundary<T> getEnd()
      Description copied from interface: ChronoInterval

      Yields the upper bound of this interval.

      Specified by:
      getEnd in interface ChronoInterval<T>
      Returns:
      end interval boundary
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: ChronoInterval

      Determines if this interval is empty.

      Specified by:
      isEmpty in interface ChronoInterval<T>
      Returns:
      true if this interval does not contain any time point else false
    • isFinite

      public boolean isFinite()
      Description copied from interface: ChronoInterval

      Determines if this interval has finite boundaries.

      Specified by:
      isFinite in interface ChronoInterval<T>
      Returns:
      true if start and end are finite else false
    • contains

      public boolean contains(T temporal)
      Description copied from interface: ChronoInterval

      Queries if given time point belongs to this interval.

      Specified by:
      contains in interface ChronoInterval<T>
      Parameters:
      temporal - time point to be queried
      Returns:
      true if given time point belongs to this interval else false
    • contains

      public boolean contains(ChronoInterval<T> other)
      Description copied from interface: ChronoInterval

      Does this interval contain the other one?

      An interval cannot contain infinite intervals but can contain an empty interval if it contains the start anchor of the empty interval.

      Specified by:
      contains in interface ChronoInterval<T>
      Parameters:
      other - another interval whose relation to this interval is to be investigated
      Returns:
      true if this interval contains the other one else false
      See Also:
      ChronoInterval.intersects(ChronoInterval)
    • isAfter

      public boolean isAfter(T temporal)
      Description copied from interface: ChronoInterval

      Is this interval after the given time point?

      Specified by:
      isAfter in interface ChronoInterval<T>
      Parameters:
      temporal - reference time point
      Returns:
      true if this interval is after given time point else false
    • isBefore

      public boolean isBefore(T temporal)
      Description copied from interface: ChronoInterval

      Is this interval before the given time point?

      Specified by:
      isBefore in interface ChronoInterval<T>
      Parameters:
      temporal - reference time point
      Returns:
      true if this interval is before given time point else false
    • isBefore

      public boolean isBefore(ChronoInterval<T> other)
      Description copied from interface: ChronoInterval

      Is this interval before the other one?

      Specified by:
      isBefore in interface ChronoInterval<T>
      Parameters:
      other - another interval whose relation to this interval is to be investigated
      Returns:
      true if this interval is before the other one else false
    • abuts

      public boolean abuts(ChronoInterval<T> other)
      Description copied from interface: ChronoInterval

      Queries if this interval abuts the other one such that there is neither any overlap nor any gap between.

      Note: Empty intervals never abut.

      Specified by:
      abuts in interface ChronoInterval<T>
      Parameters:
      other - another interval which might abut this interval
      Returns:
      true if there is no intersection and no gap between else false
    • findIntersection

      public Optional<CalendarPeriod<T>> findIntersection(ChronoInterval<T> other)

      Obtains the intersection of this interval and other one if present.

      Parameters:
      other - another interval which might have an intersection with this interval
      Returns:
      a wrapper around the found intersection or an empty wrapper
      See Also:
      Optional.isPresent(), ChronoInterval.intersects(ChronoInterval)
    • stream

      public Stream<T> stream()

      Obtains a stream for fixed calendar intervals like years, quarters, months or weeks.

      The produced stream has at least one element and is always finite. If it was produced by mean of on(...).between(...) then the step width is one calendar day.

      Returns:
      Stream
    • delta

      public long delta()

      Obtains the delta between start and end in the smallest defined units.

      If start and end are equal then the delta is zero.

      Returns:
      difference in smallest defined units
      Since:
      5.0
    • random

      public T random()

      Obtains a random date within this interval.

      Returns:
      random date within this interval
    • print

      public String print(ChronoPrinter<T> printer)

      Prints this interval using a localized interval pattern.

      If given printer does not contain a reference to a locale then the interval pattern "{0}/{1}" will be used.

      Parameters:
      printer - format object for printing start and end
      Returns:
      localized formatted string
      See Also:
      print(ChronoPrinter, String), FormatPatternProvider.getIntervalPattern(Locale)
    • print

      public String print(ChronoPrinter<T> printer, String intervalPattern)

      Prints this interval in a custom format.

      Parameters:
      printer - format object for printing start and end components
      intervalPattern - interval pattern containing placeholders {0} and {1} (for start and end)
      Returns:
      formatted string in given pattern format
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object