public abstract class Duration
extends Object
| java.lang.Object | |
| javax.xml.datatype.Duration | |
W3C XML Schema 1.0规范中定义的时间跨度的不可变表示。
持续时间对象表示格里高利时期,由六个字段(年,月,日,小时,分钟和秒)加上符号(+/-)字段组成。
前五个字段具有非负(> = 0)整数或null(表示该字段未设置),并且秒字段具有非负十进制或空值。 负号表示负的持续时间。
这个类提供了许多方法,使得使用勘误的XML Schema 1.0的持续时间数据类型更容易。
持续时间对象只有部分顺序,其中两个值A和B可能是:
例如,30天不能与一个月进行有意义的比较。 compare(Duration)方法实现了这种关系。
有关 Duration对象之间的顺序关系的详细信息,请参阅 isLongerThan(Duration)方法。
该类提供了一组基本的算术运算,如加法,减法和乘法。 由于持续时间没有全部订单,因此某些操作组合可能会失败。 例如,你不能从1个月减去15天。 查看这些方法的javadoc,了解可能发生的详细情况。
另外,由于Duration类只能处理有限精度的十进制数,因此不提供持续时间除以数字。 例如,一个不能代表1秒除以3。
但是,您可以用3乘以0.3或0.333等数字替换一个除法。
因为Duration某些操作依赖于Calendar即使Duration可以保存非常大或非常小的值,但某些方法可能无法在此类Duration上正常工作。 受影响的方法记录了他们对Calendar的依赖。
也可以看看:
Public constructors |
|
|---|---|
Duration() |
|
公共方法(Public methods) |
|
|---|---|
abstract Duration |
add(Duration rhs) 计算值为 |
abstract void |
addTo(Calendar calendar) 将此持续时间添加到 |
void |
addTo(Date date) 将此持续时间添加到 |
abstract int |
compare(Duration duration) 与此 |
boolean |
equals(Object duration) 检查此持续时间对象是否与另一个 |
int |
getDays() 获得DAYS字段的值作为整数值,如果不存在,则为0。 |
abstract Number |
getField(DatatypeConstants.Field field) 获取字段的值。 |
int |
getHours() 获取HOURS字段的值作为整数值,如果不存在,则为0。 |
int |
getMinutes() 获取MINUTES字段的值作为整数值,如果不存在,则为0。 |
int |
getMonths() 获取MONTHS字段的值作为整数值,如果不存在,则为0。 |
int |
getSeconds() 获取SECONDS字段的值作为整数值,如果不存在,则为0。 |
abstract int |
getSign() 以-1,0或1返回此持续时间的符号。 |
long |
getTimeInMillis(Calendar startInstant) 以毫秒为单位返回持续时间的长度。 |
long |
getTimeInMillis(Date startInstant) 以毫秒为单位返回持续时间的长度。 |
QName |
getXMLSchemaType() 返回此实例映射到的XML模式日期/时间类型的名称。 |
int |
getYears() 得到这个年值 |
abstract int |
hashCode() 返回与equals方法的定义一致的散列码。 |
boolean |
isLongerThan(Duration duration) 检查此持续时间对象是否严格超过另一个 |
abstract boolean |
isSet(DatatypeConstants.Field field) 检查是否设置了字段。 |
boolean |
isShorterThan(Duration duration) 检查此持续时间对象是否严格短于另一个 |
Duration |
multiply(int factor) 计算一个新的持续时间,其值比该持续时间的值长 |
abstract Duration |
multiply(BigDecimal factor) 计算一个新的持续时间,其值比该持续时间的值长 |
abstract Duration |
negate() 返回一个新 |
abstract Duration |
normalizeWith(Calendar startTimeInstant) 将特定时间点作为参考点,将年份和月份字段转换为日期字段。 |
Duration |
subtract(Duration rhs) 计算值为 |
String |
toString() 返回此 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
Duration add (Duration rhs)
计算值为 this+rhs的新持续时间。
例如,
"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
由于没有办法从1个月内有意义地减去1天,所以有些情况下 IllegalStateException中的操作失败。
形式上,计算定义如下。
首先,我们可以假设两个 Duration被增补为正不失一般性(即 (-X)+Y=Y-X , X+(-Y)=X-Y , (-X)+(-Y)=-(X+Y) )
两个正数 Duration的添加仅仅被定义为逐字段添加,其中缺少的字段被视为0。
当且仅当两个输入 Duration的相应字段 Duration设置时,结果 Duration字段将被取消设置。
请注意,如果 lhs.signum()*rhs.signum()!=-1或两者 lhs.signum()*rhs.signum()!=-1一化,则 lhs.add(rhs)将总是成功。
| 参数(Parameters) | |
|---|---|
rhs |
Duration: Duration to add to this Duration |
| 返回(Returns) | |
|---|---|
Duration |
non-null valid Duration object. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
If the rhs parameter is null. |
IllegalStateException |
If two durations cannot be meaningfully added. For example, adding negative one day to one month causes this exception. |
也可以看看:
void addTo (Calendar calendar)
将此持续时间添加到 Calendar对象。
如果这些字段存在,按年,月,日,小时,分钟,秒和毫秒的顺序拨打add(int, int) 。 由于Calendar类使用int来保存值,因此有时会出现此方法无法正常工作的情况(例如,如果字段值超出int范围)。
此外,由于此持续时间类别是格里高利持续时间,因此如果给定的 Calendar对象基于其他日历系统,则此方法无法正常工作。
超过毫秒的Duration对象的任何小数部分都将被忽略。 例如,如果这个持续时间是“P1.23456S”,那么1被添加到SECONDS,234被添加到MILLISECONDS,其余的将不被使用。
需要注意的是,因为add(int, int)是使用int, Duration与超越在其领域int范围值将导致溢/下溢给定Calendar 。 add(Duration)提供了与此方法相同的基本操作,同时避免了上溢/下溢问题。
| 参数(Parameters) | |
|---|---|
calendar |
Calendar: A calendar object whose value will be modified. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the calendar parameter is null. |
void addTo (Date date)
将此持续时间添加到 Date对象。
首先将给定日期转换为 GregorianCalendar ,然后持续时间与 addTo(Calendar)方法完全相同。
然后将更新的时刻转换回 Date对象,并用于更新给定的 Date对象。
这个有点多余的计算是明确确定月份和年份的持续时间所必需的。
| 参数(Parameters) | |
|---|---|
date |
Date: A date object whose value will be modified. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the date parameter is null. |
int compare (Duration duration)
与此 Duration实例的部分订购关系比较。
比较结果必须符合 W3C XML Schema 1.0 Part 2, Section 3.2.7.6.2, Order relation on duration 。
返回:
LESSER if this Duration is shorter than duration parameterEQUAL if this Duration is equal to duration parameterGREATER if this Duration is longer than duration parameterINDETERMINATE if a conclusive partial order relation cannot be determined| 参数(Parameters) | |
|---|---|
duration |
Duration: to compare |
| 返回(Returns) | |
|---|---|
int |
the relationship between this Durationand duration parameter as LESSER, EQUAL, GREATER or INDETERMINATE. |
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability. |
NullPointerException |
if duration is null. |
boolean equals (Object duration)
检查此持续时间对象是否与另一个 Duration对象具有相同的持续时间。
例如,“P1D”(1天)等于“PT24H”(24小时)。
当且仅当时间点t + X和t + Y对于XML Schema 1.0规范第3.2.6.2节中规定的所有测试时间点相同时,持续时间X等于Y.
请注意,有些情况下两个Duration彼此“无法比拟”,例如一个月和30天。 例如,
!new Duration("P1M").isShorterThan(new Duration("P30D"))
!new Duration("P1M").isLongerThan(new Duration("P30D"))
!new Duration("P1M").equals(new Duration("P30D"))
| 参数(Parameters) | |
|---|---|
duration |
Object: A non-null valid Duration object. |
| 返回(Returns) | |
|---|---|
boolean |
true if this duration is the same length as duration. false if duration is not a Duration object, is null, or its length is different from this duration. |
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability. |
也可以看看:
int getDays ()
获得DAYS字段的值作为整数值,如果不存在,则为0。 此方法的工作原理与getYears()类似,只不过此方法在DAYS字段中起作用。
| 返回(Returns) | |
|---|---|
int |
Days of this Duration. |
Number getField (DatatypeConstants.Field field)
获取字段的值。 持续时间对象的字段可能包含任意大的值。 因此,此方法旨在返回一个Number对象。 在YEARS,MONTHS,DAYS,HOURS和MINUTES的情况下,返回的数字将是一个非负整数。 在秒的情况下,返回的数字可能是非负的十进制值。
| 参数(Parameters) | |
|---|---|
field |
DatatypeConstants.Field: one of the six Field constants (YEARS,MONTHS,DAYS,HOURS, MINUTES, or SECONDS.) |
| 返回(Returns) | |
|---|---|
Number |
If the specified field is present, this method returns a non-null non-negative 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. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
If the field is null. |
int getHours ()
获取HOURS字段的值作为整数值,如果不存在,则为0。 此方法的工作方式与getYears()类似,只是此方法适用于HOURS字段。
| 返回(Returns) | |
|---|---|
int |
Hours of this Duration. |
int getMinutes ()
获取MINUTES字段的值作为整数值,如果不存在,则为0。 此方法的工作原理与getYears()类似,但此方法适用于MINUTES字段。
| 返回(Returns) | |
|---|---|
int |
Minutes of this Duration. |
int getMonths ()
获取MONTHS字段的值作为整数值,如果不存在,则为0。 此方法的工作原理与getYears()类似,只是此方法适用于MONTHS字段。
| 返回(Returns) | |
|---|---|
int |
Months of this Duration. |
int getSeconds ()
获取SECONDS字段的值作为整数值,如果不存在,则为0。 此方法的工作原理与getYears()类似,只不过此方法适用于SECONDS字段。
| 返回(Returns) | |
|---|---|
int |
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) |
int getSign ()
以-1,0或1返回此持续时间的符号。
| 返回(Returns) | |
|---|---|
int |
-1 if this duration is negative, 0 if the duration is zero, and 1 if the duration is positive. |
long getTimeInMillis (Calendar startInstant)
以毫秒为单位返回持续时间的长度。
如果秒字段携带的数字超过毫秒级别,则会丢弃这些数字(或换句话说,舍入为零)。例如,对于任何日历值 x ,
new Duration("PT10.00099S").getTimeInMills(x) == 10000.new Duration("-PT10.00099S").getTimeInMills(x) == -10000.
请注意,此方法使用addTo(Calendar)方法,该方法可能与其字段中值非常大的Duration对象错误地工作。 有关详细信息,请参阅addTo(Calendar)方法。
| 参数(Parameters) | |
|---|---|
startInstant |
Calendar: The length of a month/year varies. The startInstant is used to disambiguate this variance. Specifically, this method returns the difference between startInstant and startInstant+duration |
| 返回(Returns) | |
|---|---|
long |
milliseconds between startInstant and startInstant plus this Duration |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if startInstant parameter is null. |
long getTimeInMillis (Date startInstant)
以毫秒为单位返回持续时间的长度。
如果秒字段携带的数字超过毫秒级别,则会丢弃这些数字(或换句话说,舍入为零)。例如,对于任何 Date值 x ,
new Duration("PT10.00099S").getTimeInMills(x) == 10000.new Duration("-PT10.00099S").getTimeInMills(x) == -10000.
请注意,此方法使用addTo(Date)方法,该方法可能与其字段中值非常大的Duration对象错误地工作。 有关详细信息,请参阅addTo(Date)方法。
| 参数(Parameters) | |
|---|---|
startInstant |
Date: The length of a month/year varies. The startInstant is used to disambiguate this variance. Specifically, this method returns the difference between startInstant and startInstant+duration. |
| 返回(Returns) | |
|---|---|
long |
milliseconds between startInstant and startInstant plus this Duration |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
If the startInstant parameter is null. |
也可以看看:
QName getXMLSchemaType ()
返回此实例映射到的XML模式日期/时间类型的名称。 类型根据设置的字段进行计算,即isSet(DatatypeConstants.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 | ||
DURATION_YEARMONTH |
X | X | ||||
| 返回(Returns) | |
|---|---|
QName |
one of the following constants: DURATION, DURATION_DAYTIME or DURATION_YEARMONTH. |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
If the combination of set fields does not match one of the XML Schema date/time datatypes. |
int getYears ()
得到这个年值 Duration为 int或者 0 ,如果不存在。
getYears()为方便方法 getField(DatatypeConstants.YEARS) 。
As the return value is an int, an incorrect value will be returned for Durations with years that go beyond the range of an int. Use getField(DatatypeConstants.YEARS) to avoid possible loss of precision.
| 返回(Returns) | |
|---|---|
int |
If the years field is present, return its value as an int, else return 0. |
int hashCode ()
返回与equals方法的定义一致的散列码。
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object. |
也可以看看:
boolean isLongerThan (Duration duration)
检查此持续时间对象是否严格超过另一个 Duration对象。
当且仅当X> Y(如XML Schema 1.0规范的3.2.6.2节中定义)时,持续时间X比Y长。
例如,“P1D”(一天)>“PT12H”(12小时)和“P2Y”(两年)>“P23M”(23个月)。
| 参数(Parameters) | |
|---|---|
duration |
Duration: Duration to test this Duration against. |
| 返回(Returns) | |
|---|---|
boolean |
true if the duration represented by this object is longer than the given duration. false otherwise. |
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability. |
NullPointerException |
If duration is null. |
boolean isSet (DatatypeConstants.Field field)
检查是否设置了字段。 持续时间对象的字段可能存在也可能不存在。 此方法可用于测试字段是否存在。
| 参数(Parameters) | |
|---|---|
field |
DatatypeConstants.Field: one of the six Field constants (YEARS,MONTHS,DAYS,HOURS, MINUTES, or SECONDS.) |
| 返回(Returns) | |
|---|---|
boolean |
true if the field is present. false if not. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
If the field parameter is null. |
boolean isShorterThan (Duration duration)
检查此持续时间对象是否严格短于另一个 Duration对象。
| 参数(Parameters) | |
|---|---|
duration |
Duration: Duration to test this Duration against. |
| 返回(Returns) | |
|---|---|
boolean |
true if duration parameter is shorter than this Duration, else false. |
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
If the underlying implementation cannot reasonably process the request, e.g. W3C XML Schema allows for arbitrarily large/small/precise values, the request may be beyond the implementations capability. |
NullPointerException |
if duration is null. |
Duration multiply (int factor)
计算一个新的持续时间,其值比此持续时间的值长 factor倍。
This method is provided for the convenience. It is functionally equivalent to the following code:
multiply(new BigDecimal(String.valueOf(factor)))
| 参数(Parameters) | |
|---|---|
factor |
int: Factor times longer of new Duration to create. |
| 返回(Returns) | |
|---|---|
Duration |
New Duration that is factortimes longer than this Duration. |
也可以看看:
Duration multiply (BigDecimal factor)
计算一个新的持续时间,其值比该持续时间的值长 factor倍。
例如,
"P1M" (1 month) * "12" = "P12M" (12 months) "PT1M" (1 min) * "0.3" = "PT18S" (18 seconds) "P1M" (1 month) * "1.5" = IllegalStateException
由于Duration类是不可变的,因此此方法不会更改此对象的值。 它只是计算一个新的Duration对象并返回它。
该操作将以BigDecimal的精度逐场进行。 由于除秒以外的所有字段都限制为保持整数,因此计算产生的任何分数都将向下传递给下一个单元。 例如,如果您将“P1D”(1天)与“0.5”相乘,那么它将为0.5天,这将被降至“PT12H”(12小时)。 当月份的分数不能有意义地持续到几天或一年到几个月时,这将导致IllegalStateException被抛出。 例如,如果您将一个月乘以0.5。
要避免 IllegalStateException ,请使用 normalizeWith(Calendar)方法删除年份和月份字段。
| 参数(Parameters) | |
|---|---|
factor |
BigDecimal: to multiply by |
| 返回(Returns) | |
|---|---|
Duration |
returns a non-null valid Duration object |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if operation produces fraction in the months field. |
NullPointerException |
if the factor parameter is null. |
Duration negate ()
返回一个新 Duration对象,其值是 -this 。
由于Duration类是不可变的,因此此方法不会更改此对象的值。 它只是计算一个新的Duration对象并返回它。
| 返回(Returns) | |
|---|---|
Duration |
always return a non-null valid Duration object. |
Duration normalizeWith (Calendar startTimeInstant)
将特定时间点作为参考点,将年份和月份字段转换为日期字段。
例如,对于开始时间实例“2003年7月8日,17:40:32”,一个月的持续时间标准化为31天。
形式上,计算如下完成:
Calendar object by using the add(int, int) method请注意,由于Calendar类使用 int来保存年份和月份的值,因此如果此持续时间对象在年份或月份字段中包含非常大的值,则此方法可能会产生意外的结果。
| 参数(Parameters) | |
|---|---|
startTimeInstant |
Calendar: Calendar reference point. |
| 返回(Returns) | |
|---|---|
Duration |
Duration of years and months of this Duration as days. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
If the startTimeInstant parameter is null. |
Duration subtract (Duration rhs)
计算值为 this-rhs的新持续时间。
例如:
"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"
由于没有办法从1个月有意义地减去1天,所以有些情况下 IllegalStateException中的操作失败。
正式的计算定义如下。 首先,我们可以假设两个Duration均为正数而不失一般性。 (即, (-X)-Y=-(X+Y) , X-(-Y)=X+Y , (-X)-(-Y)=-(X-Y) )
然后逐场减去两个持续时间。 如果任何非零字段F的符号与最高有效字段的符号不同,则1(如果F为负)或-1(否则)将从下一个更大的单位F中借用 。
重复此过程,直到所有非零字段具有相同的符号。
如果借款发生在天数字段中(换句话说,如果计算需要借用1个月或-1个月来补偿天数),则计算失败,投掷 IllegalStateException 。
| 参数(Parameters) | |
|---|---|
rhs |
Duration: Duration to subtract from this Duration. |
| 返回(Returns) | |
|---|---|
Duration |
New Duration created from subtracting rhs from this Duration. |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
If two durations cannot be meaningfully subtracted. For example, subtracting one day from one month causes this exception. |
NullPointerException |
If the rhs parameter is null. |
也可以看看:
String toString ()
返回此 Duration Object的 String表示 Object 。
结果按照XML Schema 1.0规范进行格式化,并且总是可以稍后解析为 Duration Object newDuration(String) 。
在形式上,以下任何 Duration Object x都适用:
new Duration(x.toString()).equals(x)
| 返回(Returns) | |
|---|---|
String |
A non-null valid String representation of this Duration. |