Package org.apache.ofbiz.base.util
Class TimeDuration
- java.lang.Object
-
- org.apache.ofbiz.base.util.TimeDuration
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<TimeDuration>
- Direct Known Subclasses:
TimeDuration.NullDuration
@ThreadSafe public class TimeDuration extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<TimeDuration>
An immutable representation of a period of time.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
TimeDuration.NullDuration
-
Field Summary
Fields Modifier and Type Field Description protected int
days
protected int
hours
protected int
milliseconds
protected int
minutes
protected int
months
protected int
seconds
protected int
years
static TimeDuration
ZeroTimeDuration
ATimeDuration
instance that represents a zero time duration.
-
Constructor Summary
Constructors Constructor Description TimeDuration(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
TimeDuration(com.ibm.icu.util.Calendar cal1, com.ibm.icu.util.Calendar cal2)
Elapsed time constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description com.ibm.icu.util.Calendar
addToCalendar(com.ibm.icu.util.Calendar cal)
Add this time duration to a Calendar instance.int
compareTo(TimeDuration arg0)
int
days()
Returns the days in this time duration.boolean
equals(java.lang.Object obj)
static TimeDuration
fromLong(long duration)
Returns aTimeDuration
instance derived from an encodedlong
value.static TimeDuration
fromNumber(java.lang.Number number)
Returns aTimeDuration
instance derived from aNumber
instance.int
hashCode()
int
hours()
Returns the hours in this time duration.boolean
isNegative()
Returnstrue
if this duration is negative.boolean
isZero()
Returnstrue
if this duration is zero.int
milliseconds()
Returns the milliseconds in this time duration.int
minutes()
Returns the minutes in this time duration.int
months()
Returns the months in this time duration.static TimeDuration
parseDuration(java.lang.String duration)
int
seconds()
Returns the seconds in this time duration.static long
toLong(TimeDuration duration)
Returns along
value derived from aTimeDuration
instance.java.lang.String
toString()
Returns aString
formatted as years:months:days:hours:minutes:seconds:millseconds.int
years()
Returns the years in this time duration.
-
-
-
Field Detail
-
ZeroTimeDuration
public static final TimeDuration ZeroTimeDuration
ATimeDuration
instance that represents a zero time duration.
-
milliseconds
protected final int milliseconds
-
seconds
protected final int seconds
-
minutes
protected final int minutes
-
hours
protected final int hours
-
days
protected final int days
-
months
protected final int months
-
years
protected final int years
-
-
Constructor Detail
-
TimeDuration
public TimeDuration(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
- Parameters:
years
- The number of years in this durationmonths
- The number of months in this durationdays
- The number of days in this durationhours
- The number of hours in this durationminutes
- The number of minutes in this durationseconds
- The number of years in this durationmilliseconds
- The number of milliseconds in this duration
-
TimeDuration
public TimeDuration(com.ibm.icu.util.Calendar cal1, com.ibm.icu.util.Calendar cal2)
Elapsed time constructor. The time duration will be computed from the twoCalendar
instances.- Parameters:
cal1
-cal2
-
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
Returns aString
formatted as years:months:days:hours:minutes:seconds:millseconds.- Overrides:
toString
in classjava.lang.Object
-
compareTo
public int compareTo(TimeDuration arg0)
- Specified by:
compareTo
in interfacejava.lang.Comparable<TimeDuration>
-
isNegative
public boolean isNegative()
Returnstrue
if this duration is negative.- Returns:
true
if this duration is negative
-
isZero
public boolean isZero()
Returnstrue
if this duration is zero.- Returns:
true
if this duration is zero
-
milliseconds
public int milliseconds()
Returns the milliseconds in this time duration.
-
seconds
public int seconds()
Returns the seconds in this time duration.
-
minutes
public int minutes()
Returns the minutes in this time duration.
-
hours
public int hours()
Returns the hours in this time duration.
-
days
public int days()
Returns the days in this time duration.
-
months
public int months()
Returns the months in this time duration.
-
years
public int years()
Returns the years in this time duration.
-
addToCalendar
public com.ibm.icu.util.Calendar addToCalendar(com.ibm.icu.util.Calendar cal)
Add this time duration to a Calendar instance. Returns the original Calendar instance.- Parameters:
cal
-- Returns:
cal
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
fromLong
public static TimeDuration fromLong(long duration)
Returns aTimeDuration
instance derived from an encodedlong
value. This method is intended to be used in tandem with thetoLong
method. Note: this method should not be used to calculate elapsed time - use the elapsed time constructor instead.- Parameters:
duration
- An encoded duration- Returns:
- A
TimeDuration
instance
-
fromNumber
public static TimeDuration fromNumber(java.lang.Number number)
Returns aTimeDuration
instance derived from aNumber
instance. Ifnumber
isnull
, returns a zeroTimeDuration
.This is a convenience method intended to be used with entity engine fields. Some duration fields are stored as a
Long
, while others are stored as aDouble
. This method will decode both types.- Parameters:
number
- ANumber
instance, can benull
- Returns:
- A
TimeDuration
instance
-
parseDuration
public static TimeDuration parseDuration(java.lang.String duration)
-
toLong
public static long toLong(TimeDuration duration)
Returns along
value derived from aTimeDuration
instance. This method is intended to be used in tandem with thefromLong
method.- Parameters:
duration
-- Returns:
- the duration encoded as a
long
value
-
-