java.lang.Objectjavax.xml.datatype.Duration
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 Duration
s.
The impacted methods document their dependency on Calendar .
- href="mailto:Joseph.Fialli@Sun.COM">Joseph Fialli
- href="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi
- href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor
$
- Revision: 759828 $, $Date: 2009-03-29 21:26:29 -0400 (Sun, 29 Mar 2009) $1.5
- Method from javax.xml.datatype.Duration Summary: |
---|
add, addTo, addTo, compare, equals, getDays, getField, getHours, getMinutes, getMonths, getSeconds, getSign, getTimeInMillis, getTimeInMillis, getXMLSchemaType, getYears, hashCode, isLongerThan, isSet, isShorterThan, multiply, multiply, negate, normalizeWith, subtract, toString |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from javax.xml.datatype.Duration Detail: | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 IllegalStateException . Formally, the computation is defined as follows.
Firstly, we can assume that two
Addition of two positive
A field of the resulting
Note that | |||||||||||||||||||||||||||||||||||
Calls java.util.Calendar#add(int,int) in the order of YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, and MILLISECONDS if those fields are present. Because the Calendar class uses int to hold values, there are cases where this method won't work correctly (for example if values of fields exceed the range of int.) Also, since this duration class is a Gregorian duration, this method will not work correctly if the given Calendar object is based on some other calendar systems.
Any fractional parts of this
Note that because Calendar#add(int, int) is using
int, | |||||||||||||||||||||||||||||||||||
The given date is first converted into a java.util.GregorianCalendar , then the duration is added exactly like the #addTo(Calendar) method. The updated time instant is then converted back into a Date object and used to update the given Date object. This somewhat redundant computation is necessary to unambiguously determine the duration of months and years. | |||||||||||||||||||||||||||||||||||
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: | |||||||||||||||||||||||||||||||||||
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")) | |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
Returns the length of the duration in milliseconds. If the seconds field carries more digits than millisecond 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 #addTo(Calendar) method,
which may work incorrectly with | |||||||||||||||||||||||||||||||||||
Returns the length of the duration in milliseconds. If the seconds field carries more digits than millisecond order,
those will be simply discarded (or in other words, rounded to zero.)
For example, for any
Note that this method uses the #addTo(Date) method,
which may work incorrectly with | |||||||||||||||||||||||||||||||||||
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. #isSet(DatatypeConstants.Field field) ==
| |||||||||||||||||||||||||||||||||||
Get the years value of this
As the return value is an | |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
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). | |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
Checks if this duration object is strictly shorter than
another | |||||||||||||||||||||||||||||||||||
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))) | |||||||||||||||||||||||||||||||||||
factor times
longer than the value of this duration.
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 BigDecimal . Since all the fields except seconds are restricted to hold integers, any fraction produced by the computation will be carried down toward the next lower unit. For example, if you multiply "P1D" (1 day) with "0.5", then it will be 0.5 day, which will be carried down to "PT12H" (12 hours). When fractions of month cannot be meaningfully carried down to days, or year to months, this will cause an IllegalStateException to be thrown. For example if you multiple one month by 0.5. To avoid IllegalStateException , use the #normalizeWith(Calendar) method to remove the years and months fields. | |||||||||||||||||||||||||||||||||||
Duration object whose
value is -this .
Since the | |||||||||||||||||||||||||||||||||||
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 | |||||||||||||||||||||||||||||||||||
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 IllegalStateException . 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 F is different from the sign of the most significant field, 1 (if F is negative) or -1 (otherwise) will be borrowed from the next bigger unit of F. 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 IllegalStateException . | |||||||||||||||||||||||||||||||||||
Returns a The result is formatted according to the XML Schema 1.0 specification and can be always parsed back later into the
equivalent Formally, the following holds for any new Duration(x.toString()).equals(x) |