Class TimestampInterval

All Implemented Interfaces:
Serializable, ChronoInterval<PlainTimestamp>

public final class TimestampInterval extends IsoInterval<PlainTimestamp,​TimestampInterval> implements Serializable

Defines a timestamp interval on local timeline.

Since:
2.0
Author:
Meno Hochschild
See Also:
Serialized Form
  • Field Details

    • ALWAYS

      public static final TimestampInterval ALWAYS
      Constant for a timestamp interval from infinite past to infinite future.
      Since:
      3.36/4.31
  • Method Details

    • comparator

      public static Comparator<ChronoInterval<PlainTimestamp>> comparator()

      Defines a comparator which sorts intervals first by start boundary and then by length.

      Returns:
      Comparator
      Throws:
      IllegalArgumentException - if applied on intervals which have boundaries with extreme values
      Since:
      2.0
    • between

      public static TimestampInterval between(PlainTimestamp start, PlainTimestamp end)

      Creates a finite half-open interval between given time points.

      Parameters:
      start - timestamp of lower boundary (inclusive)
      end - timestamp of upper boundary (exclusive)
      Returns:
      new timestamp interval
      Throws:
      IllegalArgumentException - if start is after end
      Since:
      2.0
    • between

      public static TimestampInterval between(LocalDateTime start, LocalDateTime end)

      Creates a finite half-open interval between given time points.

      Parameters:
      start - timestamp of lower boundary (inclusive)
      end - timestamp of upper boundary (exclusive)
      Returns:
      new timestamp interval
      Throws:
      IllegalArgumentException - if start is after end
      Since:
      4.11
      See Also:
      between(PlainTimestamp, PlainTimestamp)
    • since

      public static TimestampInterval since(PlainTimestamp start)

      Creates an infinite half-open interval since given start timestamp.

      Parameters:
      start - timestamp of lower boundary (inclusive)
      Returns:
      new timestamp interval
      Since:
      2.0
    • since

      public static TimestampInterval since(LocalDateTime start)

      Creates an infinite half-open interval since given start timestamp.

      Parameters:
      start - timestamp of lower boundary (inclusive)
      Returns:
      new timestamp interval
      Since:
      4.11
      See Also:
      since(PlainTimestamp)
    • until

      public static TimestampInterval until(PlainTimestamp end)

      Creates an infinite open interval until given end timestamp.

      Parameters:
      end - timestamp of upper boundary (exclusive)
      Returns:
      new timestamp interval
      Since:
      2.0
    • until

      public static TimestampInterval until(LocalDateTime end)

      Creates an infinite open interval until given end timestamp.

      Parameters:
      end - timestamp of upper boundary (exclusive)
      Returns:
      new timestamp interval
      Since:
      4.11
      See Also:
      until(PlainTimestamp)
    • from

      public static TimestampInterval from(ChronoInterval<PlainTimestamp> interval)

      Converts an arbitrary timestamp interval to an interval of this type.

      Parameters:
      interval - any kind of timestamp interval
      Returns:
      TimestampInterval
      Since:
      3.34/4.29
    • getStartAsTimestamp

      public PlainTimestamp getStartAsTimestamp()

      Yields the start time point.

      Returns:
      start time point or null if infinite
      Since:
      4.11
      See Also:
      Boundary.isInfinite()
    • getStartAsLocalDateTime

      public LocalDateTime getStartAsLocalDateTime()

      Yields the start time point.

      Returns:
      start time point or null if infinite
      Since:
      4.11
      See Also:
      Boundary.isInfinite()
    • getEndAsTimestamp

      public PlainTimestamp getEndAsTimestamp()

      Yields the end time point.

      Returns:
      end time point or null if infinite
      Since:
      4.11
      See Also:
      Boundary.isInfinite()
    • getEndAsLocalDateTime

      public LocalDateTime getEndAsLocalDateTime()

      Yields the end time point.

      Returns:
      end time point or null if infinite
      Since:
      4.11
      See Also:
      Boundary.isInfinite()
    • atUTC

      public MomentInterval atUTC()

      Combines this local timestamp interval with the timezone offset UTC+00:00 to a global UTC-interval.

      Returns:
      global timestamp interval interpreted at offset UTC+00:00
      Since:
      2.0
      See Also:
      at(ZonalOffset)
    • at

      public MomentInterval at(ZonalOffset offset)

      Combines this local timestamp interval with given timezone offset to a global UTC-interval.

      Parameters:
      offset - timezone offset
      Returns:
      global timestamp interval interpreted at given offset
      Since:
      2.0
      See Also:
      atUTC(), inTimezone(TZID)
    • inStdTimezone

      public MomentInterval inStdTimezone()

      Combines this local timestamp interval with the system timezone to a global UTC-interval.

      Returns:
      global timestamp interval interpreted in system timezone
      Since:
      2.0
      See Also:
      Timezone.ofSystem()
    • inTimezone

      public MomentInterval inTimezone(TZID tzid)

      Combines this local timestamp interval with given timezone to a global UTC-interval.

      Parameters:
      tzid - timezone id
      Returns:
      global timestamp interval interpreted in given timezone
      Throws:
      IllegalArgumentException - if given timezone cannot be loaded
      Since:
      2.0
      See Also:
      Timezone.of(TZID), inStdTimezone(), GapResolver.NEXT_VALID_TIME, OverlapResolver.EARLIER_OFFSET
    • getDuration

      @SafeVarargs public final <U extends IsoUnit> Duration<U> getDuration(U... units)

      Yields the length of this interval in given units.

      Type Parameters:
      U - generic unit type
      Parameters:
      units - time units to be used in calculation
      Returns:
      duration in given units
      Throws:
      UnsupportedOperationException - if this interval is infinite
      Since:
      2.0
    • getDuration

      public Duration<IsoUnit> getDuration(Timezone tz, IsoUnit... units)

      Yields the length of this interval in given units and applies a timezone offset correction .

      Parameters:
      tz - timezone
      units - time units to be used in calculation
      Returns:
      duration in given units including a zonal correction
      Throws:
      UnsupportedOperationException - if this interval is infinite
      Since:
      2.0
    • move

      public TimestampInterval move(long amount, IsoUnit unit)

      Moves this interval along the time axis by given units.

      Parameters:
      amount - amount of units
      unit - time unit for moving
      Returns:
      moved copy of this interval
    • stream

      public Stream<PlainTimestamp> stream(Duration<?> duration)

      Obtains a stream iterating over every timestamp which is the result of addition of given duration to start until the end of this interval is reached.

      The stream size is limited to Integer.MAX_VALUE - 1 else an ArithmeticException will be thrown.

      Parameters:
      duration - duration which has to be added to the start multiple times
      Returns:
      stream consisting of distinct timestamps which are the result of adding the duration to the start
      Throws:
      IllegalArgumentException - if the duration is not positive
      IllegalStateException - if this interval is infinite or if there is no canonical form
      Since:
      4.18
      See Also:
      IsoInterval.toCanonical(), stream(Duration, PlainTimestamp, PlainTimestamp)
    • stream

      public static Stream<PlainTimestamp> stream(Duration<?> duration, PlainTimestamp start, PlainTimestamp end)

      Obtains a stream iterating over every timestamp which is the result of addition of given duration to start until the end is reached.

      This static method avoids the costs of constructing an instance of TimestampInterval. The stream size is limited to Integer.MAX_VALUE - 1 else an ArithmeticException will be thrown.

      Parameters:
      duration - duration which has to be added to the start multiple times
      start - start boundary - inclusive
      end - end boundary - exclusive
      Returns:
      stream consisting of distinct timestamps which are the result of adding the duration to the start
      Throws:
      IllegalArgumentException - if start is after end or if the duration is not positive
      Since:
      4.18
    • streamPartitioned

      public Stream<TimestampInterval> streamPartitioned(DayPartitionRule rule)

      Creates a partitioning stream of timestamp intervals where every day of this interval is partitioned according to given partitioning rule.

      If a day of this interval is not fully defined (concerns only start and end) then it will be intersected with given day partition rule.

      Parameters:
      rule - day partition rule
      Returns:
      stream of timestamp intervals
      Throws:
      IllegalStateException - if this interval is infinite or if there is no canonical form
      Since:
      5.0
    • random

      public PlainTimestamp random()

      Obtains a random timestamp within this interval.

      Returns:
      random timestamp within this interval
      Throws:
      IllegalStateException - if this interval is infinite or empty or if there is no canonical form
      Since:
      5.0
      See Also:
      IsoInterval.toCanonical()
    • formatISO

      public String formatISO(IsoDateStyle dateStyle, IsoDecimalStyle decimalStyle, ClockUnit precision, InfinityStyle infinityStyle)

      Prints the canonical form of this interval in given ISO-8601 style.

      Parameters:
      dateStyle - iso-compatible date style
      decimalStyle - iso-compatible decimal style
      precision - controls the precision of output format with constant length
      infinityStyle - controlling the format of infinite boundaries
      Returns:
      String
      Throws:
      IllegalStateException - if there is no canonical form or given infinity style prevents infinite intervals
      Since:
      4.18
      See Also:
      IsoInterval.toCanonical()
    • formatReduced

      public String formatReduced(IsoDateStyle dateStyle, IsoDecimalStyle decimalStyle, ClockUnit precision, InfinityStyle infinityStyle)

      Prints the canonical form of this interval in given reduced ISO-8601 style.

      The term "reduced" means that higher-order elements like the year can be left out in the end component if it is equal to the value of the start component. Example:

           TimestampInterval interval =
                TimestampInterval.between(
                    PlainTimestamp.of(2016, 2, 29, 10, 45, 53),
                    PlainTimestamp.of(2016, 2, 29, 16, 30));
           System.out.println(
                interval.formatReduced(
                    IsoDateStyle.EXTENDED_CALENDAR_DATE, IsoDecimalStyle.DOT, ClockUnit.MINUTES, InfinityStyle.SYMBOL));
           // Output: 2016-02-29T10:45/T16:30
       
      Parameters:
      dateStyle - iso-compatible date style
      decimalStyle - iso-compatible decimal style
      precision - controls the precision of output format with constant length
      infinityStyle - controlling the format of infinite boundaries
      Returns:
      String
      Throws:
      IllegalStateException - if there is no canonical form or given infinity style prevents infinite intervals
      Since:
      4.18
      See Also:
      IsoInterval.toCanonical()
    • parse

      public static TimestampInterval parse(String text, ChronoParser<PlainTimestamp> parser) throws ParseException

      Interpretes given text as interval using a localized interval pattern.

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

      Parameters:
      text - text to be parsed
      parser - format object for parsing start and end components
      Returns:
      parsed interval
      Throws:
      IndexOutOfBoundsException - if given text is empty
      ParseException - if the text is not parseable
      Since:
      3.9/4.6
      See Also:
      parse(String, ChronoParser, String), FormatPatternProvider.getIntervalPattern(Locale)
    • parse

      public static TimestampInterval parse(String text, ChronoParser<PlainTimestamp> parser, String intervalPattern) throws ParseException

      Interpretes given text as interval using given interval pattern.

      About usage see also DateInterval.parse(String, ChronoParser, String).

      Parameters:
      text - text to be parsed
      parser - format object for parsing start and end components
      intervalPattern - interval pattern containing placeholders {0} and {1} (for start and end)
      Returns:
      parsed interval
      Throws:
      IndexOutOfBoundsException - if given text is empty
      ParseException - if the text is not parseable
      Since:
      3.9/4.6
    • parse

      public static TimestampInterval parse(CharSequence text, ChronoParser<PlainTimestamp> parser, BracketPolicy policy) throws ParseException

      Interpretes given text as interval.

      This method can also accept a hyphen as alternative to solidus as separator between start and end component unless the start component is a period. Infinity symbols are understood.

      Parameters:
      text - text to be parsed
      parser - format object for parsing start and end components
      policy - strategy for parsing interval boundaries
      Returns:
      result
      Throws:
      ParseException - if parsing does not work
      IndexOutOfBoundsException - if the start position is at end of text or even behind
      Since:
      4.18
    • parse

      public static TimestampInterval parse(CharSequence text, ChronoParser<PlainTimestamp> startFormat, char separator, ChronoParser<PlainTimestamp> endFormat, BracketPolicy policy, ParseLog status)

      Interpretes given text as interval.

      This method is mainly intended for parsing technical interval formats similar to ISO-8601 which are not localized. Infinity symbols are understood.

      Parameters:
      text - text to be parsed
      startFormat - format object for parsing start component
      separator - char separating start and end component
      endFormat - format object for parsing end component
      policy - strategy for parsing interval boundaries
      status - parser information (always as new instance)
      Returns:
      result or null if parsing does not work
      Throws:
      IndexOutOfBoundsException - if the start position is at end of text or even behind
      Since:
      3.9/4.6
    • parseISO

      public static TimestampInterval parseISO(String text) throws ParseException

      Interpretes given ISO-conforming text as interval.

      All styles are supported, namely calendar dates, ordinal dates and week dates, either in basic or in extended format. Mixed date styles for start and end are not allowed however. Furthermore, one of start or end can also be represented by a period string. If not then the end component may exist in an abbreviated form as documented in ISO-8601-paper leaving out higher-order elements like the calendar year (which will be overtaken from the start component instead). Infinity symbols are understood as extension although strictly spoken ISO-8601 does not know or specify infinite intervals. Examples for supported formats:

        System.out.println(
            TimestampInterval.parseISO(
                "2012-01-01T14:15/2014-06-20T16:00"));
        // output: [2012-01-01T14:15/2014-06-20T16:00)
      
        System.out.println(
            TimestampInterval.parseISO(
                "2012-01-01T14:15/08-11T16:00"));
        // output: [2012-01-01T14:15/2012-08-11T16:00)
      
        System.out.println(
            TimestampInterval.parseISO(
                "2012-01-01T14:15/16:00"));
        // output: [2012-01-01T14:15/2012-01-01T16:00)
      
        System.out.println(
            TimestampInterval.parseISO(
                "2012-01-01T14:15/P2DT1H45M"));
        // output: [2012-01-01T14:15/2012-01-03T16:00)
      
        System.out.println(
            TimestampInterval.parseISO(
                "2015-01-01T08:45/-"));
        // output: [2015-01-01T08:45:00/+∞)
       

      This method dynamically creates an appropriate interval format for reduced forms. If performance is more important then a static fixed formatter might be considered.

      Parameters:
      text - text to be parsed
      Returns:
      parsed interval
      Throws:
      IndexOutOfBoundsException - if given text is empty
      ParseException - if the text is not parseable
      Since:
      2.0
      See Also:
      BracketPolicy.SHOW_NEVER