|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ix.util.Duration
public class Duration
The difference between two dates. (There must be a decent Duration class out there somewhere, but I was unable to find it and so wrote this one. There is a lot that it doesn't do, but it allows durations to be created and to be output in a readable form. It also supports most of the XML Schema / ISO 8601 syntax for durations.)
Durations should be understood as periods of time without fixed endpoints, such as 10 minutes or 3 hours.
The full XML Schema duration datatype is difficult to work with, because the number of days in a month varies. Hence durations aren't even totally ordered. However, in the 1.1 version of the Schema definition, there are two derived types that are each totally ordered: yearMonthDuration and dayTimeDuration. That also means that full durations can be represented by two values: months and seconds. In the earlier 1.0 version, durations were in a 6-dimensional space: years, months, days, hours, minutes, and seconds. That had the unfortunate consequence that 1 day and 24 hours ("PT1D" and "PT24H") were considered different values rather than two (lexical) representations of the same value.
This class avoids some of the complexity by disallowing year and month values, thus making it close to the dayTimeDuration type. For long durations, the number of days can become large instead. However, this class retains the idea of a multi-dimensional space and instances needn't always have the number of hours be less than 24, the number of minutes less than 60, and so on. Users should not reply on the details of this class's behaviour in this area, because they are likely to change.
The canonical numeric representation of an instance of this class is as a long holding the total number of milliseconds in the period of time represented by the duration. The breakdown into days, hours, minutes, etc is normally visible only when a Duration is converted to a String.
Nested Class Summary | |
---|---|
static class |
Duration.SyntaxException
Describes a syntax error found when constructing a duration from a string. |
Field Summary | |
---|---|
protected long |
days
|
protected int |
hours
|
protected LList |
isoSequence
|
protected long |
milliseconds
|
protected int |
minutes
|
protected int |
ms
|
protected boolean |
negative
|
protected int |
seconds
|
Constructor Summary | |
---|---|
Duration(java.util.Date from,
java.util.Date to)
Constructs a duration by taking the difference of two dates. |
|
Duration(long milliseconds)
Constructs a duration that corresponds to the specified number of milliseconds. |
|
Duration(long days,
int hours,
int minutes,
int seconds,
int ms)
Constructs a duration from the specified component times. |
|
Duration(java.lang.String isoString)
Constructs a duration from a string written in a subset of the ISO 8601 notation used for the XML Schema duration datatype. |
Method Summary | |
---|---|
long |
asMilliseconds()
Return the total number of milliseconds represented by this Duration. |
protected void |
breakup(long total)
Separate the total milliseconds into days, hours, minutes, ... |
protected long |
combine(long days,
int hours,
int minutes,
int seconds,
int ms)
Combine the components into a total number of milliseconds. |
int |
compareTo(java.lang.Object obj)
Compares two durations. |
boolean |
equals(java.lang.Object obj)
Determines whether two durations represent the same amount of time. |
int |
hashCode()
Returns a hash value for this duration. |
static void |
main(java.lang.String[] argv)
Simple test program. |
protected void |
parseISOString(java.lang.String s)
Fills in fields in this duration by parsing a string that is written in the subset of ISO 8601 notation that is supported by this class. |
Duration |
round(long granularity)
Returns this Duration after rounding the time to the nearest multiple of the specified granularity. |
Duration |
roundToMinutes()
Equivalent to round(60 * 1000). |
Duration |
roundToSeconds()
Equivalent to round(1000). |
java.lang.String |
toISOString()
Returns a string that represents this duration in ISO 8601 syntax. |
java.lang.String |
toString()
Returns an easy-to-read description of this duration. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected long milliseconds
protected boolean negative
protected long days
protected int hours
protected int minutes
protected int seconds
protected int ms
protected LList isoSequence
Constructor Detail |
---|
public Duration(java.util.Date from, java.util.Date to)
new Duration(to.getTime() - from.getTime())
public Duration(long milliseconds)
asMilliseconds()
method. This constructor
always produces a duration in which the number of hours
is less than 24, the number of minutes is less than 60,
and so on.
public Duration(long days, int hours, int minutes, int seconds, int ms)
public Duration(java.lang.String isoString)
The full syntax is PnYnMnDTnHnMnS, optionally preceded by a minus sign, where each n is a possibly different unsigned number, 'Y', 'M', 'D', 'H', 'M, and 'S' stand for years, months, days, hours, minutes, and seconds respectively, 'P' stands for "period", and 'T' separates the year-month date portion from the time portion. 'P' must always be present, and 'T' must be present if any hours, minutes, or seconds are specified. The number-letter combinations are all optional, so long as at least one is specified. All of the numbers must have no decimal point except for the number of seconds. None of the numbers is restricted in range.
This class does not support year or month values and they must not be given. Second fractions are stored only to the nearest millisecond, although they can be written to arbitrary precision.
Duration.SyntaxException
- if the string does not
conform to the required syntax.toISOString()
,
parseISOString(String)
Method Detail |
---|
protected void breakup(long total)
protected long combine(long days, int hours, int minutes, int seconds, int ms)
public Duration round(long granularity)
Duration d = new Duration(...).round(60 * 1000);
granularity
- milliseconds, usually a value that represents
one larger unit such as a day, hour, or minute.roundToSeconds()
,
roundToMinutes()
public Duration roundToSeconds()
public Duration roundToMinutes()
public long asMilliseconds()
Duration(long)
constructor.
public java.lang.String toISOString()
Duration(String)
constructor. It will return only strings that can be parsed
by that constructor, but if s is the string returned for this
duration, it is not absolutely guaranteed that
new Duration(s).toISOString().equals(s)However, this duration and one greated from s will have the same value as milliseconds.
toString()
protected void parseISOString(java.lang.String s)
Duration(String)
constructor.
Duration.SyntaxException
- if the string does not
conform to the required syntax.public java.lang.String toString()
toString
in class java.lang.Object
toISOString()
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
java.lang.ClassCastException
- if the argument is not a Duration.public int compareTo(java.lang.Object obj)
compareTo
in interface java.lang.Comparable
java.lang.ClassCastException
- if the argument is not a Duration.public static void main(java.lang.String[] argv)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |