Class MomentInterval
- All Implemented Interfaces:
Serializable
,ChronoInterval<Moment>
Defines a moment interval on global timeline.
- Since:
- 2.0
- Author:
- Meno Hochschild
- See Also:
- Serialized Form
-
Field Summary
Modifier and TypeFieldDescriptionstatic MomentInterval
Constant for a moment interval from infinite past to infinite future.static double
Determines the alignment of surrounding intervals.static double
Determines the alignment of surrounding intervals.static double
Determines the alignment of surrounding intervals. -
Method Summary
Modifier and TypeMethodDescriptionstatic MomentInterval
Creates a finite half-open interval between given time points.static MomentInterval
Creates a finite half-open interval between given time points.static Comparator<ChronoInterval<Moment>>
Defines a comparator which sorts intervals first by start boundary and then by length.formatISO(IsoDateStyle dateStyle, IsoDecimalStyle decimalStyle, ClockUnit precision, ZonalOffset offset, InfinityStyle infinityStyle)
Prints the canonical form of this interval in given ISO-8601 style.formatReduced(IsoDateStyle dateStyle, IsoDecimalStyle decimalStyle, ClockUnit precision, ZonalOffset offset, InfinityStyle infinityStyle)
Prints the canonical form of this interval in given reduced ISO-8601 style.static MomentInterval
from(ChronoInterval<Moment> interval)
Converts an arbitrary moment interval to an interval of this type.Yields the end time point.Yields the end time point.getNominalDuration(Timezone tz, IsoUnit... units)
Yields the nominal duration of this interval in given timezone and units.Yields the length of this interval on the UTC-scale.Yields the length of this interval on the POSIX-scale.Yields the start time point.Yields the start time point.Moves this interval along the POSIX-axis by given time units.Moves this interval along the UTC-axis by given SI-units.static MomentInterval
parse(CharSequence text, ChronoParser<Moment> startFormat, char separator, ChronoParser<Moment> endFormat, BracketPolicy policy, ParseLog status)
Interpretes given text as interval.static MomentInterval
parse(CharSequence text, ChronoParser<Moment> parser, BracketPolicy policy)
Interpretes given text as interval.static MomentInterval
parse(String text, ChronoParser<Moment> parser)
Interpretes given text as interval using a localized interval pattern.static MomentInterval
parse(String text, ChronoParser<Moment> parser, String intervalPattern)
Interpretes given text as interval using given interval pattern.static MomentInterval
Interpretes given ISO-conforming text as interval.random()
Obtains a random moment within this interval.static MomentInterval
Creates an infinite half-open interval since given start.static MomentInterval
Creates an infinite half-open interval since given start.stream(MachineTime<?> duration)
Obtains a stream iterating over every moment which is the result of addition of given duration to start until the end of this interval is reached.stream(MachineTime<?> duration, Moment start, Moment end)
Obtains a stream iterating over every moment which is the result of addition of given duration to start until the end is reached.static MomentInterval
surrounding(Instant instant, Duration duration, double alignment)
Creates an interval surrounding given instant.static MomentInterval
surrounding(Moment moment, MachineTime<?> duration, double alignment)
Creates an interval surrounding given moment.Converts this instance to a local timestamp interval in the system timezone.toZonalInterval(String tzid)
Converts this instance to a zonal timestamp interval in given timezone.toZonalInterval(TZID tzid)
Converts this instance to a zonal timestamp interval in given timezone.static MomentInterval
Creates an infinite open interval until given end.static MomentInterval
Creates an infinite open interval until given end.Methods inherited from class net.time4j.range.IsoInterval
abuts, collapse, contains, contains, enclosedBy, encloses, equals, equivalentTo, findIntersection, finishedBy, finishes, get, getEnd, getStart, hashCode, intersects, isAfter, isAfter, isBefore, isBefore, isEmpty, meets, metBy, overlappedBy, overlaps, precededBy, precedes, print, print, print, print, startedBy, starts, toCanonical, toString, withClosedEnd, withClosedStart, withEnd, withEnd, withOpenEnd, withOpenStart, withStart, withStart, withValue
Methods inherited from interface net.time4j.range.ChronoInterval
isFinite
-
Field Details
-
ALWAYS
Constant for a moment interval from infinite past to infinite future.- Since:
- 3.36/4.31
-
LEFT_ALIGNED
public static double LEFT_ALIGNEDDetermines the alignment of surrounding intervals.- See Also:
surrounding(Moment, MachineTime, double)
-
CENTERED
public static double CENTEREDDetermines the alignment of surrounding intervals.- See Also:
surrounding(Moment, MachineTime, double)
-
RIGHT_ALIGNED
public static double RIGHT_ALIGNEDDetermines the alignment of surrounding intervals.- See Also:
surrounding(Moment, MachineTime, double)
-
-
Method Details
-
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
Creates a finite half-open interval between given time points.
- Parameters:
start
- moment of lower boundary (inclusive)end
- moment of upper boundary (exclusive)- Returns:
- new moment interval
- Throws:
IllegalArgumentException
- if start is after end- Since:
- 2.0
-
between
Creates a finite half-open interval between given time points.
- Parameters:
start
- moment of lower boundary (inclusive)end
- moment of upper boundary (exclusive)- Returns:
- new moment interval
- Throws:
IllegalArgumentException
- if start is after end- Since:
- 4.11
- See Also:
between(Moment, Moment)
-
since
Creates an infinite half-open interval since given start.
- Parameters:
start
- moment of lower boundary (inclusive)- Returns:
- new moment interval
- Since:
- 2.0
-
since
Creates an infinite half-open interval since given start.
- Parameters:
start
- moment of lower boundary (inclusive)- Returns:
- new moment interval
- Since:
- 4.11
- See Also:
since(Moment)
-
until
Creates an infinite open interval until given end.
- Parameters:
end
- moment of upper boundary (exclusive)- Returns:
- new moment interval
- Since:
- 2.0
-
until
Creates an infinite open interval until given end.
- Parameters:
end
- moment of upper boundary (exclusive)- Returns:
- new moment interval
- Since:
- 4.11
- See Also:
until(Moment)
-
surrounding
Creates an interval surrounding given moment.
Alignment:
- If the alignment is
0.0
then the new interval will start at given moment. - If the alignment is
0.5
then the new interval will be centered around given moment. - If the alignment is
1.0
then the new interval will end at given moment.
- Parameters:
moment
- embedded moment at focus of alignmentduration
- machine time durationalignment
- determines how to align the interval around moment (in range0.0 <= x <= 1.0
)- Returns:
- new moment interval
- Throws:
IllegalArgumentException
- if the duration is negative or the alignment is not finite or out of rangeUnsupportedOperationException
- if any given or calculated moment is before 1972 (only for SI-duration)- Since:
- 3.34/4.29
- See Also:
LEFT_ALIGNED
,CENTERED
,RIGHT_ALIGNED
- If the alignment is
-
surrounding
Creates an interval surrounding given instant.
Equivalent to
surrounding(Moment.from(instant), MachineTime.from(duration), alignment)
.- Parameters:
instant
- embedded instant at focus of alignmentduration
- machine time durationalignment
- determines how to align the interval around instant (in range0.0 <= x <= 1.0
)- Returns:
- new moment interval
- Throws:
IllegalArgumentException
- if the duration is negative or the alignment is not finite or out of range- Since:
- 4.31
- See Also:
LEFT_ALIGNED
,CENTERED
,RIGHT_ALIGNED
-
from
Converts an arbitrary moment interval to an interval of this type.
- Parameters:
interval
- any kind of moment interval- Returns:
- MomentInterval
- Since:
- 3.34/4.29
-
getStartAsMoment
Yields the start time point.
- Returns:
- start time point or
null
if infinite - Since:
- 4.11
- See Also:
Boundary.isInfinite()
-
getStartAsInstant
Yields the start time point.
Note: Leap seconds will be lost here.
- Returns:
- start time point or
null
if infinite - Since:
- 4.11
- See Also:
Boundary.isInfinite()
-
getEndAsMoment
Yields the end time point.
- Returns:
- end time point or
null
if infinite - Since:
- 4.11
- See Also:
Boundary.isInfinite()
-
getEndAsInstant
Yields the end time point.
Note: Leap seconds will be lost here.
- Returns:
- end time point or
null
if infinite - Since:
- 4.11
- See Also:
Boundary.isInfinite()
-
toLocalInterval
Converts this instance to a local timestamp interval in the system timezone.
- Returns:
- local timestamp interval in system timezone (leap seconds will always be lost)
- Since:
- 2.0
- See Also:
Timezone.ofSystem()
,toZonalInterval(TZID)
,toZonalInterval(String)
-
toZonalInterval
Converts this instance to a zonal timestamp interval in given timezone.
- Parameters:
tzid
- timezone id- Returns:
- zonal timestamp interval in given timezone (leap seconds will always be lost)
- Throws:
IllegalArgumentException
- if given timezone cannot be loaded- Since:
- 2.0
- See Also:
toLocalInterval()
-
toZonalInterval
Converts this instance to a zonal timestamp interval in given timezone.
- Parameters:
tzid
- timezone id- Returns:
- zonal timestamp interval in given timezone (leap seconds will always be lost)
- Throws:
IllegalArgumentException
- if given timezone cannot be loaded- Since:
- 2.0
- See Also:
toZonalInterval(TZID)
,toLocalInterval()
-
getNominalDuration
Yields the nominal duration of this interval in given timezone and units.
- Parameters:
tz
- timezoneunits
- time units to be used in calculation- Returns:
- nominal duration
- Throws:
UnsupportedOperationException
- if this interval is infinite- Since:
- 3.0
- See Also:
getSimpleDuration()
,getRealDuration()
-
getSimpleDuration
Yields the length of this interval on the POSIX-scale.
- Returns:
- machine time duration on POSIX-scale
- Throws:
UnsupportedOperationException
- if this interval is infinite- Since:
- 2.0
- See Also:
getRealDuration()
-
getRealDuration
Yields the length of this interval on the UTC-scale.
- Returns:
- machine time duration on UTC-scale
- Throws:
UnsupportedOperationException
- if start is before year 1972 or if this interval is infinite- Since:
- 2.0
- See Also:
getSimpleDuration()
-
move
Moves this interval along the POSIX-axis by given time units.
- Parameters:
amount
- amount of unitsunit
- time unit for moving- Returns:
- moved copy of this interval
-
move
Moves this interval along the UTC-axis by given SI-units.
- Parameters:
amount
- amount of unitsunit
- time unit for moving- Returns:
- moved copy of this interval
-
stream
Obtains a stream iterating over every moment 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 anArithmeticException
will be thrown.- Parameters:
duration
- duration which has to be added to the start multiple times- Returns:
- stream consisting of distinct moments which are the result of adding the duration to the start
- Throws:
IllegalArgumentException
- if the duration is not positiveIllegalStateException
- if this interval is infinite or if there is no canonical form- Since:
- 4.35
- See Also:
IsoInterval.toCanonical()
,stream(MachineTime, Moment, Moment)
-
stream
Obtains a stream iterating over every moment 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
MomentInterval
. The stream size is limited toInteger.MAX_VALUE - 1
else anArithmeticException
will be thrown.- Parameters:
duration
- duration which has to be added to the start multiple timesstart
- start boundary - inclusiveend
- end boundary - exclusive- Returns:
- stream consisting of distinct moments 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.35
-
random
Obtains a random moment within this interval.
- Returns:
- random moment 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, ZonalOffset offset, InfinityStyle infinityStyle)Prints the canonical form of this interval in given ISO-8601 style.
- Parameters:
dateStyle
- iso-compatible date styledecimalStyle
- iso-compatible decimal styleprecision
- controls the precision of output format with constant lengthoffset
- timezone offsetinfinityStyle
- 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, ZonalOffset offset, 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. And the offset is never printed in the end component of finite intervals. Example:
MomentInterval interval = MomentInterval.between( PlainTimestamp.of(2012, 6, 29, 10, 45), PlainTimestamp.of(2012, 6, 30, 23, 59, 59).atUTC().plus(1, SI.SECONDS)); System.out.println( interval.formatReduced( IsoDateStyle.EXTENDED_CALENDAR_DATE, IsoDecimalStyle.DOT, ClockUnit.SECONDS, ZonalOffset.UTC, InfinityStyle.SYMBOL)); // Output: 2016-02-29T10:45:00Z/30T23:59:60
- Parameters:
dateStyle
- iso-compatible date styledecimalStyle
- iso-compatible decimal styleprecision
- controls the precision of output format with constant lengthoffset
- timezone offsetinfinityStyle
- 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
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 parsedparser
- format object for parsing start and end components- Returns:
- parsed interval
- Throws:
IndexOutOfBoundsException
- if given text is emptyParseException
- if the text is not parseable- Since:
- 3.9/4.6
- See Also:
parse(String, ChronoParser, String)
,FormatPatternProvider.getIntervalPattern(Locale)
-
parse
public static MomentInterval parse(String text, ChronoParser<Moment> parser, String intervalPattern) throws ParseExceptionInterpretes given text as interval using given interval pattern.
About usage see also
DateInterval.parse(String, ChronoParser, String)
.- Parameters:
text
- text to be parsedparser
- format object for parsing start and end componentsintervalPattern
- interval pattern containing placeholders {0} and {1} (for start and end)- Returns:
- parsed interval
- Throws:
IndexOutOfBoundsException
- if given text is emptyParseException
- if the text is not parseable- Since:
- 3.9/4.6
-
parse
public static MomentInterval parse(CharSequence text, ChronoParser<Moment> parser, BracketPolicy policy) throws ParseExceptionInterpretes 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 parsedparser
- format object for parsing start and end componentspolicy
- strategy for parsing interval boundaries- Returns:
- result
- Throws:
ParseException
- if parsing does not workIndexOutOfBoundsException
- if the start position is at end of text or even behind- Since:
- 4.18
-
parse
public static MomentInterval parse(CharSequence text, ChronoParser<Moment> startFormat, char separator, ChronoParser<Moment> 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 parsedstartFormat
- format object for parsing start componentseparator
- char separating start and end componentendFormat
- format object for parsing end componentpolicy
- strategy for parsing interval boundariesstatus
- 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
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). In latter case, the timezone offset of the end component is optional, too. 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( MomentInterval.parseISO( "2012-01-01T14:15Z/2014-06-20T16:00Z")); // output: [2012-01-01T14:15:00Z/2014-06-20T16:00:00Z) System.out.println( MomentInterval.parseISO( "2012-01-01T14:15Z/08-11T16:00+00:01")); // output: [2012-01-01T14:15:00Z/2012-08-11T15:59:00Z) System.out.println( MomentInterval.parseISO( "2012-01-01T14:15Z/16:00")); // output: [2012-01-01T14:15:00Z/2012-01-01T16:00:00Z) System.out.println( MomentInterval.parseISO( "2012-01-01T14:15Z/P2DT1H45M")); // output: [2012-01-01T14:15:00Z/2012-01-03T16:00:00Z) System.out.println( MomentInterval.parseISO( "2015-01-01T08:45Z/-")); // output: [2015-01-01T08:45:00Z/+∞)
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 emptyParseException
- if the text is not parseable- Since:
- 2.0
- See Also:
BracketPolicy.SHOW_NEVER
-