com.melloware.jukes.util
Class TimeSpan

java.lang.Object
  extended by com.melloware.jukes.util.TimeSpan
All Implemented Interfaces:
Serializable, Cloneable, Comparable

public class TimeSpan
extends Object
implements Comparable, Serializable, Cloneable

The value of an instance of TimeSpan represents a period of time. TimeSpan can be used in several ways. To calculate the difference in time between two dates:

TimeSpan timespan = TimeSpan.subtract(date1, date2);
To add five days to a TimeSpan:
timspan.add(TimeSpanUnit.DAYS, 5);
To subtract another TimeSpan object from this one:
timspan.subtract(timespan2);

Author:
Thomas Paul
See Also:
Serialized Form

Field Summary
static TimeSpan MAX_VALUE
          Represents the Maximum TimeSpan value
static TimeSpan MIN_VALUE
          Represents the Minimum TimeSpan value
static TimeSpan ZERO
          Represents the TimeSpan with a value of zero
 
Constructor Summary
TimeSpan(long time)
          Creates a new instance of TimeSpan based on the number of milliseconds entered.
TimeSpan(TimeSpanUnit unit, long value)
          Creates a new TimeSpan object based on the unit and value entered.
 
Method Summary
 void add(TimeSpan timespan)
          Adds a TimeSpan to this TimeSpan.
 void add(TimeSpanUnit unit, long value)
          Adds a number of units to this TimeSpan.
 Object clone()
          Returns a clone of this TimeSpan.
static int compare(TimeSpan first, TimeSpan second)
          Compares two TimeSpan objects.
 int compareTo(Object o)
          Compares this object with the specified object for order.
 TimeSpan duration()
          Returns a TimeSpan whose value is the absolute value of this TimeSpan.
 boolean equals(Object obj)
          Indicates whether some other object is "equal to" this one.
 long getDays()
          Gets the number of days (truncated).
 long getHours()
          Gets the number of hours (truncated).
 long getMilliseconds()
          Gets the number of milliseconds.
 long getMinutes()
          Gets the number of minutes (truncated).
 String getMusicDuration()
          Returns a string for music formatting for album length.
 long getSeconds()
          Gets the number of seconds (truncated).
 long getTime()
          Gets the time.
 double getTotalDays()
          Gets the number of days including fractional days.
 double getTotalHours()
          Gets the number of hours including fractional hours.
 double getTotalMinutes()
          Gets the number of minutes including fractional minutes.
 double getTotalSeconds()
          Gets the number of seconds including fractional seconds.
 int hashCode()
          Returns a hash code value for the object.
 boolean isNegative()
          Indicates whether the value of the TimeSpan is negative.
 boolean isPositive()
          Indicates whether the value of the TimeSpan is positive.
 boolean isZero()
          Indicates whether the value of the TimeSpan is zero.
 TimeSpan negate()
          Returns a TimeSpan whose value is the negated value of this TimeSpan.
 void setTime(long aTime)
          Sets the time.
static TimeSpan subtract(Date date1, Date date2)
          Subtracts two Date objects creating a new TimeSpan object.
 void subtract(TimeSpan timespan)
          Subtracts a TimeSpan from this TimeSpan.
 void subtract(TimeSpanUnit unit, long value)
          Subtracts a number of units from this TimeSpan.
 String toString()
          Returns a string representation of the object in the format "[-]d.hh:mm:ss.ff" where "-" is an optional sign for negative TimeSpan values, the "d" component is days, "hh" is hours, "mm" is minutes, "ss" is seconds, and "ff" is milliseconds
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_VALUE

public static final TimeSpan MAX_VALUE
Represents the Maximum TimeSpan value


MIN_VALUE

public static final TimeSpan MIN_VALUE
Represents the Minimum TimeSpan value


ZERO

public static final TimeSpan ZERO
Represents the TimeSpan with a value of zero

Constructor Detail

TimeSpan

public TimeSpan(long time)
Creates a new instance of TimeSpan based on the number of milliseconds entered.

Parameters:
time - the number of milliseconds for this TimeSpan.

TimeSpan

public TimeSpan(TimeSpanUnit unit,
                long value)
Creates a new TimeSpan object based on the unit and value entered.

Parameters:
unit - the type of unit to use to create a TimeSpan instance.
value - the number of units to use to create a TimeSpan instance.
Method Detail

compare

public static int compare(TimeSpan first,
                          TimeSpan second)
Compares two TimeSpan objects.

Parameters:
first - first TimeSpan to use in the compare.
second - second TimeSpan to use in the compare.
Returns:
a negative integer, zero, or a positive integer as the first TimeSpan is less than, equal to, or greater than the second TimeSpan.

subtract

public static TimeSpan subtract(Date date1,
                                Date date2)
Subtracts two Date objects creating a new TimeSpan object.

Parameters:
date1 - Date to use as the base value.
date2 - Date to subtract from the base value.
Returns:
a TimeSpan object representing the difference bewteen the two Date objects.

getDays

public long getDays()
Gets the number of days (truncated).

Returns:
the number of days.

getHours

public long getHours()
Gets the number of hours (truncated).

Returns:
the number of hours.

getMilliseconds

public long getMilliseconds()
Gets the number of milliseconds.

Returns:
the number of milliseconds.

getMinutes

public long getMinutes()
Gets the number of minutes (truncated).

Returns:
the number of minutes.

getMusicDuration

public String getMusicDuration()
Returns a string for music formatting for album length. So it would look like 48:03 or 74:15.

Returns:
the string value of the music duration

getSeconds

public long getSeconds()
Gets the number of seconds (truncated).

Returns:
the number of seconds.

getTotalDays

public double getTotalDays()
Gets the number of days including fractional days.

Returns:
the number of days.

getTotalHours

public double getTotalHours()
Gets the number of hours including fractional hours.

Returns:
the number of hours.

getTotalMinutes

public double getTotalMinutes()
Gets the number of minutes including fractional minutes.

Returns:
the number of minutes.

getTotalSeconds

public double getTotalSeconds()
Gets the number of seconds including fractional seconds.

Returns:
the number of seconds.

isNegative

public boolean isNegative()
Indicates whether the value of the TimeSpan is negative.

Returns:
true if the value of the TimeSpan is less than zero. false otherwise.

isPositive

public boolean isPositive()
Indicates whether the value of the TimeSpan is positive.

Returns:
true if the value of the TimeSpan is greater than zero. false otherwise.

isZero

public boolean isZero()
Indicates whether the value of the TimeSpan is zero.

Returns:
true if the value of the TimeSpan is equal to zero. false otherwise.

add

public void add(TimeSpan timespan)
Adds a TimeSpan to this TimeSpan.

Parameters:
timespan - the TimeSpan to add to this TimeSpan.

add

public void add(TimeSpanUnit unit,
                long value)
Adds a number of units to this TimeSpan.

Parameters:
unit - the type of unit to add to this TimeSpan.
value - the number of units to add to this TimeSpan.

clone

public Object clone()
Returns a clone of this TimeSpan.

Overrides:
clone in class Object
Returns:
a clone of this TimeSpan.

compareTo

public int compareTo(Object o)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Comparison is based on the number of milliseconds in this TimeSpan.

Specified by:
compareTo in interface Comparable
Parameters:
o - the Object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
ClassCastException - if the specified object's type prevents it from being compared to this Object.

duration

public TimeSpan duration()
Returns a TimeSpan whose value is the absolute value of this TimeSpan.

Returns:
a TimeSpan whose value is the absolute value of this TimeSpan.

equals

public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one. Comparison is based on the number of milliseconds in this TimeSpan.

Overrides:
equals in class Object
Parameters:
obj - the reference object with which to compare.
Returns:
true if the obj argument is a TimeSpan object with the exact same number of milliseconds. false otherwise.

hashCode

public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The method uses the same algorithm as found in the Long class.

Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable

negate

public TimeSpan negate()
Returns a TimeSpan whose value is the negated value of this TimeSpan.

Returns:
a TimeSpan whose value is the negated value of this TimeSpan.

subtract

public void subtract(TimeSpan timespan)
Subtracts a TimeSpan from this TimeSpan.

Parameters:
timespan - the TimeSpan to subtract from this TimeSpan.

subtract

public void subtract(TimeSpanUnit unit,
                     long value)
Subtracts a number of units from this TimeSpan.

Parameters:
unit - the type of unit to subtract from this TimeSpan.
value - the number of units to subtract from this TimeSpan.

toString

public String toString()
Returns a string representation of the object in the format "[-]d.hh:mm:ss.ff" where "-" is an optional sign for negative TimeSpan values, the "d" component is days, "hh" is hours, "mm" is minutes, "ss" is seconds, and "ff" is milliseconds

Overrides:
toString in class Object
Returns:
a string containing the number of milliseconds.

getTime

public long getTime()
Gets the time.

Returns:
Returns the time.

setTime

public void setTime(long aTime)
Sets the time.

Parameters:
aTime - The time to set.


Copyright © 1999-2010 Melloware Inc. All Rights Reserved.