Immutable representation of a time span as defined in the W3C XML Schema 1.0 specification.
A Duration object represents a period of Gregorian time, which consists of six fields (years, months, days, hours, minutes, and seconds) plus a sign (+/-) field.
The first five fields have non-negative (>=0) integers or null (which represents that the field is not set), and the seconds field has a non-negative decimal or null. A negative sign indicates a negative duration.
This class provides a number of methods that make it easy to use for the duration datatype of XML Schema 1.0 with the errata.
Duration objects only have partial order, where two values A and B maybe either:
For example, 30 days cannot be meaningfully compared to one month.
The compare(Duration duration) method implements this
relationship.
See the isLongerThan(Duration) method for details about
the order relationship among Duration objects.
This class provides a set of basic arithmetic operations, such as addition, subtraction and multiplication. Because durations don't have total order, an operation could fail for some combinations of operations. For example, you cannot subtract 15 days from 1 month. See the javadoc of those methods for detailed conditions where this could happen.
Also, division of a duration by a number is not provided because
the Duration class can only deal with finite precision
decimal numbers. For example, one cannot represent 1 sec divided by 3.
However, you could substitute a division by 3 with multiplying by numbers such as 0.3 or 0.333.
Because some operations of Duration rely on Calendar
even though Duration can hold very large or very small values,
some of the methods may not work correctly on such Durations.
The impacted methods document their dependency on Calendar.
XMLGregorianCalendar#add(Duration)
| Modifier and Type | Field and Description |
|---|---|
| private static final boolean | DEBUG
Debugging |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public abstract Duration | |
| public abstract void | |
| public void | |
| public abstract int | Returns: the relationship betweenthis Duration and duration parameter as
DatatypeConstants#LESSER, DatatypeConstants#EQUAL, DatatypeConstants#GREATER
or DatatypeConstants#INDETERMINATE.to compare duration)Partial order relation comparison with this |
| public boolean | Returns: true if this duration is the same length as
duration.
false if duration is null,
is not a
Duration object,
or its length is different from this duration.The object to compare this duration)Duration against.Overrides java. Checks if this duration object has the same duration
as another |
| private static long | Returns: Milliseconds ofcal.Calendar to get time in milliseconds.Calls the |
| public int | Returns: Days of thisDuration.Obtains the value of the DAYS field as an integer value, or 0 if not present. |
| public abstract Number | Returns: If the specified field is present, this method returns a non-null non-negativeNumber object that
represents its value. If it is not present, return null.
For YEARS, MONTHS, DAYS, HOURS, and MINUTES, this method
returns a java. object. For SECONDS, this
method returns a java..one of the six Field constants (YEARS,MONTHS,DAYS,HOURS,
MINUTES, or SECONDS.) fieldGets the value of a field. |
| public int | Returns: Hours of thisDuration.Obtains the value of the HOURS field as an integer value, or 0 if not present. |
| public int | Returns: Minutes of thisDuration.Obtains the value of the MINUTES field as an integer value, or 0 if not present. |
| public int | Returns: Months of thisDuration.Obtains the value of the MONTHS field as an integer value, or 0 if not present. |
| public int | Returns: seconds in the integer value. The fraction of seconds will be discarded (for example, if the actual value is 2.5, this method returns 2)Obtains the value of the SECONDS field as an integer value, or 0 if not present. |
| public abstract int | Returns: -1 if this duration is negative, 0 if the duration is zero, and 1 if the duration is positive.Returns the sign of this duration in -1,0, or 1. |
| public long | Returns: milliseconds betweenstartInstant and
startInstant plus this DurationThe length of a month/year varies. The startInstant)startInstant is
used to disambiguate this variance. Specifically, this method
returns the difference between startInstant and
startInstant+durationReturns the length of the duration in milli-seconds. |
| public long | Returns: milliseconds betweenstartInstant and
startInstant plus this DurationThe length of a month/year varies. The startInstant)startInstant is
used to disambiguate this variance. Specifically, this method
returns the difference between startInstant and
startInstant+duration.Returns the length of the duration in milli-seconds. |
| public QName | Returns: one of the following constants:DatatypeConstants#DURATION,
DatatypeConstants#DURATION_DAYTIME or
DatatypeConstants#DURATION_YEARMONTH.Return the name of the XML Schema date/time type that this instance maps to. |
| public int | Returns: If the years field is present, return its value as anint, else return 0.Get the years value of this |
| public abstract int | hashCode()
Overrides java. Returns a hash code consistent with the definition of the equals method. |
| public boolean | Returns: true if the duration represented by this object is longer than the given duration. false otherwise.Duration to test this Duration against.Checks if this duration object is strictly longer than
another |
| public abstract boolean | Returns: true if the field is present. false if not.one of the six Field constants (YEARS,MONTHS,DAYS,HOURS,
MINUTES, or SECONDS.) fieldChecks if a field is set. |
| public boolean | Returns: true if duration parameter is shorter than this Duration,
else false.Duration to test this Duration against.Checks if this duration object is strictly shorter than
another |
| public Duration | Returns: NewDuration that is factortimes longer than this Duration.Factor times longer of new factor)Duration to create.Computes a new duration whose value is |
| public abstract Duration | Returns: returns a non-null validDuration objectto multiply by factor)Computes a new duration whose value is |
| public abstract Duration | Returns: always return a non-null validDuration object.Returns a new |
| public abstract Duration | Returns: Duration of years and months of this Duration as days.Calendar reference point.Converts the years and months fields into the days field by using a specific time instant as the reference point. |
| public Duration | |
| public String | Returns: A non-null valid String representation of this Duration.Overrides java. Returns a |
| private String | Returns: String representation of BigDecimalBigDecimal to format as a StringTurns |
| DEBUG | back to summary |
|---|---|
| private static final boolean DEBUG Debugging | |
| Duration | back to summary |
|---|---|
| public Duration() Default no-arg constructor. Note Always use the | |
| add | back to summary |
|---|---|
| public abstract Duration add(final Duration rhs) Computes a new duration whose value is For example, "1 day" + "-3 days" = "-2 days" "1 year" + "1 day" = "1 year and 1 day" "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)" "15 hours" + "-3 days" = "-(2 days,9 hours)" "1 year" + "-1 day" = IllegalStateException Since there's no way to meaningfully subtract 1 day from 1 month,
there are cases where the operation fails in
Formally, the computation is defined as follows.
Firstly, we can assume that two
Addition of two positive
A field of the resulting
Note that
| |
| addTo | back to summary |
|---|---|
| public abstract void addTo(Calendar calendar) Adds this duration to a
Calls
Also, since this duration class is a Gregorian duration, this
method will not work correctly if the given
Any fractional parts of this
Note that because
| |
| addTo | back to summary |
|---|---|
| public void addTo(Date date) Adds this duration to a
The given date is first converted into
a
The updated time instant is then converted back into a
This somewhat redundant computation is necessary to unambiguously determine the duration of months and years.
| |
| compare | back to summary |
|---|---|
| public abstract int compare(final Duration duration) Partial order relation comparison with this Comparison result must be in accordance with W3C XML Schema 1.0 Part 2, Section 3.2.7.6.2, Order relation on duration. Return:
| |
| equals | back to summary |
|---|---|
| public boolean equals(final Object duration) Overrides java. Checks if this duration object has the same duration
as another For example, "P1D" (1 day) is equal to "PT24H" (24 hours). Duration X is equal to Y if and only if time instant t+X and t+Y are the same for all the test time instants specified in the section 3.2.6.2 of the XML Schema 1.0 specification. Note that there are cases where two
!new Duration("P1M").isShorterThan(new Duration("P30D"))
!new Duration("P1M").isLongerThan(new Duration("P30D"))
!new Duration("P1M").equals(new Duration("P30D"))
| |
| getCalendarTimeInMillis | back to summary |
|---|---|
| private static long getCalendarTimeInMillis(final Calendar cal) Calls the Todo In future, this should be replaced by
| |
| getDays | back to summary |
|---|---|
| public int getDays() Obtains the value of the DAYS field as an integer value,
or 0 if not present.
This method works just like
| |
| getField | back to summary |
|---|---|
| public abstract Number getField(final DatatypeConstants. Gets the value of a field.
Fields of a duration object may contain arbitrary large value.
Therefore this method is designed to return a
| |
| getHours | back to summary |
|---|---|
| public int getHours() Obtains the value of the HOURS field as an integer value,
or 0 if not present.
This method works just like
| |
| getMinutes | back to summary |
|---|---|
| public int getMinutes() Obtains the value of the MINUTES field as an integer value,
or 0 if not present.
This method works just like
| |
| getMonths | back to summary |
|---|---|
| public int getMonths() Obtains the value of the MONTHS field as an integer value,
or 0 if not present.
This method works just like
| |
| getSeconds | back to summary |
|---|---|
| public int getSeconds() Obtains the value of the SECONDS field as an integer value,
or 0 if not present.
This method works just like
| |
| getSign | back to summary |
|---|---|
| public abstract int getSign() Returns the sign of this duration in -1,0, or 1.
| |
| getTimeInMillis | back to summary |
|---|---|
| public long getTimeInMillis(final Calendar startInstant) Returns the length of the duration in milli-seconds. If the seconds field carries more digits than milli-second order,
those will be simply discarded (or in other words, rounded to zero.)
For example, for any Calendar value
Note that this method uses the
| |
| getTimeInMillis | back to summary |
|---|---|
| public long getTimeInMillis(final Date startInstant) Returns the length of the duration in milli-seconds. If the seconds field carries more digits than milli-second order,
those will be simply discarded (or in other words, rounded to zero.)
For example, for any
Note that this method uses the
| |
| getXMLSchemaType | back to summary | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| public QName getXMLSchemaType() Return the name of the XML Schema date/time type that this instance
maps to. Type is computed based on fields that are set,
i.e.
| |||||||||||||||||||||||||||||
| getYears | back to summary |
|---|---|
| public int getYears() Get the years value of this
As the return value is an
| |
| hashCode | back to summary |
|---|---|
| public abstract int hashCode() Overrides java. Returns a hash code consistent with the definition of the equals method.
| |
| isLongerThan | back to summary |
|---|---|
| public boolean isLongerThan(final Duration duration) Checks if this duration object is strictly longer than
another Duration X is "longer" than Y if and only if X > Y as defined in the section 3.2.6.2 of the XML Schema 1.0 specification. For example, "P1D" (one day) > "PT12H" (12 hours) and "P2Y" (two years) > "P23M" (23 months).
| |
| isSet | back to summary |
|---|---|
| public abstract boolean isSet(final DatatypeConstants. Checks if a field is set. A field of a duration object may or may not be present. This method can be used to test if a field is present.
| |
| isShorterThan | back to summary |
|---|---|
| public boolean isShorterThan(final Duration duration) Checks if this duration object is strictly shorter than
another
| |
| multiply | back to summary |
|---|---|
| public Duration multiply(int factor) Computes a new duration whose value is This method is provided for the convenience. It is functionally equivalent to the following code: multiply(new BigDecimal(String.valueOf(factor)))
| |
| multiply | back to summary |
|---|---|
| public abstract Duration multiply(final BigDecimal factor) Computes a new duration whose value is For example, "P1M" (1 month) * "12" = "P12M" (12 months) "PT1M" (1 min) * "0.3" = "PT18S" (18 seconds) "P1M" (1 month) * "1.5" = IllegalStateException
Since the
The operation will be performed field by field with the precision
of
To avoid
| |
| negate | back to summary |
|---|---|
| public abstract Duration negate() Returns a new
Since the
| |
| normalizeWith | back to summary |
|---|---|
| public abstract Duration normalizeWith(final Calendar startTimeInstant) Converts the years and months fields into the days field by using a specific time instant as the reference point. For example, duration of one month normalizes to 31 days given the start time instance "July 8th 2003, 17:40:32". Formally, the computation is done as follows:
Note that since the Calendar class uses
| |
| subtract | back to summary |
|---|---|
| public Duration subtract(final Duration rhs) Computes a new duration whose value is For example: "1 day" - "-3 days" = "4 days" "1 year" - "1 day" = IllegalStateException "-(1 hour,50 minutes)" - "-20 minutes" = "-(1hours,30 minutes)" "15 hours" - "-3 days" = "3 days and 15 hours" "1 year" - "-1 day" = "1 year and 1 day" Since there's no way to meaningfully subtract 1 day from 1 month,
there are cases where the operation fails in Formally the computation is defined as follows.
First, we can assume that two Then two durations are subtracted field by field.
If the sign of any non-zero field This process is repeated until all the non-zero fields have the same sign. If a borrow occurs in the days field (in other words, if
the computation needs to borrow 1 or -1 month to compensate
days), then the computation fails by throwing an
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a The result is formatted according to the XML Schema 1.0 spec
and can be always parsed back later into the
equivalent Formally, the following holds for any new Duration(x.toString()).equals(x)
| |
| toString | back to summary |
|---|---|
| private String toString(BigDecimal bd) Turns Due to a behavior change in the
| |