public class XMLDuration
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
static int |
DURATION |
static int |
DURATION_DAYTIME |
static int |
DURATION_YEARMONTH |
static int |
EQUAL |
static int |
GREATER |
static int |
INDETERMINATE |
static int |
LESSER |
Constructor and Description |
---|
XMLDuration() |
XMLDuration(java.lang.String a_duration) |
XMLDuration(XMLDuration a_duration) |
Modifier and Type | Method and Description |
---|---|
int |
compare(XMLDuration a_duration)
Partial order relation comparison with this
Duration instance. |
boolean |
equals(java.lang.Object a_duration) |
long |
getDays() |
java.lang.Number |
getField(int a_field)
Gets the value of a field.
|
java.lang.Number |
getField(java.lang.Object a_field) |
static java.lang.String |
getFieldName(int a_field) |
static java.lang.String |
getFieldName(java.lang.Object a_field) |
java.util.Enumeration |
getFields() |
long |
getHours() |
int |
getLastFieldSet() |
long |
getMinutes() |
long |
getMonths() |
double |
getSeconds() |
int |
getSign()
Returns the sign of this duration in -1,0, or 1.
|
java.lang.String |
getXMLSchema() |
int |
getXMLSchemaType()
Return the name of the XML Schema date/time type that this instance
maps to.
|
long |
getYears() |
int |
hashCode() |
boolean |
isLongerThan(XMLDuration a_duration)
Checks if this duration object is strictly longer than
another
Duration object. |
boolean |
isSet(int a_field)
Checks if a field is set.
|
boolean |
isShorterThan(XMLDuration a_duration)
Checks if this duration object is strictly shorter than
another
Duration object. |
XMLDuration |
negate()
Returns a new
Duration object whose
value is -this . |
java.lang.String |
toString()
Returns a
String representation of this Duration Object . |
public static final int DURATION
public static final int DURATION_DAYTIME
public static final int DURATION_YEARMONTH
public static final int LESSER
public static final int EQUAL
public static final int GREATER
public static final int INDETERMINATE
public XMLDuration()
public XMLDuration(XMLDuration a_duration)
public XMLDuration(java.lang.String a_duration) throws XMLParseException
XMLParseException
public java.lang.String getXMLSchema()
public int getXMLSchemaType() throws java.lang.IllegalStateException
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(int a_field)
== true
.
Required fields for XML Schema 1.0 Date/Time Datatypes. (timezone is optional for all date/time datatypes) |
||||||
---|---|---|---|---|---|---|
Datatype | year | month | day | hour | minute | second |
DURATION | X | X | X | X | X | X |
DURATION_DAYTIME | X | X | X | X | ||
URATION_YEARMONTH | X | X |
java.lang.IllegalStateException
- If the combination of set fields does not match one of the XML Schema date/time datatypes.public int getSign()
public long getYears()
public long getMonths()
public long getDays()
public long getHours()
public long getMinutes()
public double getSeconds()
public static java.lang.String getFieldName(java.lang.Object a_field)
public static java.lang.String getFieldName(int a_field)
public java.util.Enumeration getFields()
public java.lang.Number getField(java.lang.Object a_field)
public java.lang.Number getField(int a_field)
Number
object.
In case of YEARS, MONTHS, DAYS, HOURS, and MINUTES, the returned
number will be a non-negative integer. In case of seconds,
the returned number may be a non-negative decimal value.field
- one of the six Field constants (YEARS,MONTHS,DAYS,HOURS,
MINUTES, or SECONDS.)Number
object that
represents its value. If it is not present, return null.
For YEARS, MONTHS, DAYS, HOURS, and MINUTES, this method
returns a BigInteger
object. For SECONDS, this
method returns a BigDecimal
.java.lang.NullPointerException
- If the field
is null
.public boolean isSet(int a_field)
field
- one of the six Field constants (YEARS,MONTHS,DAYS,HOURS,
MINUTES, or SECONDS.)public XMLDuration negate()
Duration
object whose
value is -this
.
Since the Duration
class is immutable, this method
doesn't change the value of this object. It simply computes
a new Duration object and returns it.
Duration
object.public int compare(XMLDuration a_duration)
Partial order relation comparison with this Duration
instance.
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:
Duration
is shorter than duration
parameterDuration
is equal to duration
parameterDuration
is longer than duration
parameterduration
- to comparethis
Duration
and duration
parameter as
LESSER, EQUAL,GREATER or INDETERMINATE.java.lang.NullPointerException
- if duration
is null
.#isShorterThan(Duration)
,
#isLongerThan(Duration)
public boolean equals(java.lang.Object a_duration)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean isLongerThan(XMLDuration a_duration)
Checks if this duration object is strictly longer than
another Duration
object.
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).
duration
- Duration
to test this Duration
against.java.lang.NullPointerException
- If duration
is null.#isShorterThan(Duration)
,
#compare(Duration duration)
public boolean isShorterThan(XMLDuration a_duration)
Checks if this duration object is strictly shorter than
another Duration
object.
duration
- Duration
to test this Duration
against.true
if duration
parameter is shorter than this Duration
,
else false
.java.lang.NullPointerException
- if duration
is null.#isLongerThan(Duration duration)
,
#compare(Duration duration)
public int getLastFieldSet()
public java.lang.String toString()
Returns a String
representation of this Duration
Object
.
The result is formatted according to the XML Schema 1.0 spec and can be always parsed back later into the
equivalent Duration
Object
.
Formally, the following holds for any Duration
Object
x:
new Duration(x.toString()).equals(x)
toString
in class java.lang.Object
null
valid String
representation of this Duration
.Copyright © 2023. All rights reserved.