Class Duration
- java.lang.Object
-
- org.alfresco.service.cmr.repository.datatype.Duration
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable
@AlfrescoPublicApi public class Duration extends java.lang.Object implements java.lang.Comparable, java.io.SerializableThis data type represents duration/interval/period as defined by the XMLSchema type duration. The lexical representation of duration is PnYnMnDTnHnMnS. P is a literal value that starts the expression nY is an integer number of years followed by the literal Y nM is an integer number of months followed by the literal M nD is an integer number of days followed by the literal D T is the literal that separates the date and time nH is an integer number of hours followed by a literal H nM is an integer number of minutes followed by a literal M nS is a decimal number of seconds followed by a literal S Any numbers and designator may be absent if the value is zero. A minus sign may appear before the literal P to indicate a negative duration. If no time items are present the literal T must not appear. This implementation is immutable and thread safe. There are two forms of duration common on database types. The code contains warnings wheer these are relevant.- Author:
- andyh
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static DurationDAYstatic DurationMONTHstatic DurationQUARTERstatic DurationSIX_MONTHSstatic DurationTWO_WEEKSstatic DurationWEEKstatic java.lang.StringXML_DAYstatic java.lang.StringXML_MONTHstatic java.lang.StringXML_QUARTERstatic java.lang.StringXML_SIX_MONTHSstatic java.lang.StringXML_TWO_WEEKSstatic java.lang.StringXML_WEEKstatic java.lang.StringXML_YEARstatic DurationYEAR
-
Constructor Summary
Constructors Constructor Description Duration()Constructor for Duration - a zero value durationDuration(boolean positive_in, long months_in, long seconds_in, long nanos_in)Construct a duration from months seconds and nanos Checks sign and fixes up seconds and nano.Duration(java.lang.String duration)Construct a Duration from the XMLSchema definitionDuration(java.lang.String start, java.lang.String end)Create a duration betweeen two dates expressed as strings.Duration(java.util.Date date)Create a duration given a date.Duration(java.util.Date start_in, java.util.Date end_in)Construct a preiod between the two given dates
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.util.Dateadd(java.util.Date date, Duration duration)Add a duration to a date and return the date plus the specified increment.Durationadd(Duration add)Add two durations togetherintcompareTo(java.lang.Object o)Compare two durationsDurationdivide(int d)Divide the duration - if year-month drops the day-second part of the durationbooleanequals(java.lang.Object o)java.lang.StringformattedString()Format in human readable form TODO: I18nDurationgetDayToYear()Extract the day to sec part.DurationgetYearToMonth()Extract the year to month partinthashCode()booleanhasTime()Check if it includes timebooleanisDayToSec()Check if is day-secbooleanisYearToMonth()Check if is year-monthjava.lang.StringlargestComponentformattedString()static voidmain(java.lang.String[] args)TODO: Tests that should be moved into a unit teststatic java.util.Datesubtract(java.util.Date date, Duration duration)Subtract a period for a given dateDurationsubtract(Duration sub)Subtract one duration from anotherjava.lang.StringtoString()Produce the XML Schema stringDurationunaryMinus()Negate the duration
-
-
-
Field Detail
-
XML_DAY
public static final java.lang.String XML_DAY
- See Also:
- Constant Field Values
-
XML_WEEK
public static final java.lang.String XML_WEEK
- See Also:
- Constant Field Values
-
XML_TWO_WEEKS
public static final java.lang.String XML_TWO_WEEKS
- See Also:
- Constant Field Values
-
XML_MONTH
public static final java.lang.String XML_MONTH
- See Also:
- Constant Field Values
-
XML_QUARTER
public static final java.lang.String XML_QUARTER
- See Also:
- Constant Field Values
-
XML_SIX_MONTHS
public static final java.lang.String XML_SIX_MONTHS
- See Also:
- Constant Field Values
-
XML_YEAR
public static final java.lang.String XML_YEAR
- See Also:
- Constant Field Values
-
DAY
public static final Duration DAY
-
WEEK
public static final Duration WEEK
-
TWO_WEEKS
public static final Duration TWO_WEEKS
-
MONTH
public static final Duration MONTH
-
QUARTER
public static final Duration QUARTER
-
SIX_MONTHS
public static final Duration SIX_MONTHS
-
YEAR
public static final Duration YEAR
-
-
Constructor Detail
-
Duration
public Duration()
Constructor for Duration - a zero value duration
-
Duration
public Duration(java.lang.String duration)
Construct a Duration from the XMLSchema definition
-
Duration
public Duration(java.util.Date date)
Create a duration given a date. The duration is between the two dates provided. Sadly, it works out the duration by incrementing the lower calendar until it matches the higher.
-
Duration
public Duration(java.lang.String start, java.lang.String end)Create a duration betweeen two dates expressed as strings. Uses the standard XML date form.- Parameters:
start- - the date at the start of the periodend- - the date at the end of the period
-
Duration
public Duration(java.util.Date start_in, java.util.Date end_in)Construct a preiod between the two given dates- Parameters:
start_in- Dateend_in- Date
-
Duration
public Duration(boolean positive_in, long months_in, long seconds_in, long nanos_in)Construct a duration from months seconds and nanos Checks sign and fixes up seconds and nano. Treats year-month abd day-sec as separate chunks
-
-
Method Detail
-
add
public static java.util.Date add(java.util.Date date, Duration duration)Add a duration to a date and return the date plus the specified increment.- Parameters:
date- - the initial dateduration- - the duration to add on to the date (the duration may be negative)- Returns:
- the adjusted date.
-
subtract
public static java.util.Date subtract(java.util.Date date, Duration duration)Subtract a period for a given date- Parameters:
date- - the intial dateduration- - the diration to subtract- Returns:
- the adjusted date.
-
unaryMinus
public Duration unaryMinus()
Negate the duration
-
divide
public Duration divide(int d)
Divide the duration - if year-month drops the day-second part of the duration
-
isYearToMonth
public boolean isYearToMonth()
Check if is year-month
-
isDayToSec
public boolean isDayToSec()
Check if is day-sec
-
hasTime
public boolean hasTime()
Check if it includes time
-
getYearToMonth
public Duration getYearToMonth()
Extract the year to month part
-
getDayToYear
public Duration getDayToYear()
Extract the day to sec part.
-
compareTo
public int compareTo(java.lang.Object o)
Compare two durations- Specified by:
compareToin interfacejava.lang.Comparable
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object- See Also:
Object.equals(Object)
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object- See Also:
Object.hashCode()
-
toString
public java.lang.String toString()
Produce the XML Schema string- Overrides:
toStringin classjava.lang.Object- See Also:
Object.toString()
-
largestComponentformattedString
public java.lang.String largestComponentformattedString()
-
formattedString
public java.lang.String formattedString()
Format in human readable form TODO: I18n
-
main
public static void main(java.lang.String[] args)
TODO: Tests that should be moved into a unit test- Parameters:
args- String[]
-
-