pulse - the web application framework Get pulse at SourceForge.net. Fast, secure and Free Open Source software downloads Ohloh project report for pulse-java

org.torweg.pulse.util.time
Class TimeSpan

java.lang.Object
  extended by org.torweg.pulse.util.time.TimeSpan
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<TimeSpan>

@Embeddable
public final class TimeSpan
extends java.lang.Object
implements java.lang.Comparable<TimeSpan>, java.io.Serializable

represents a time span in human readable format.

Numbers directly followed by the character y are interpreted as years. The character d represents days, h represents hours, m represents minutes and s represents seconds. If a number is followed by a space or is at the end of the time span string, it is interpreted as milliseconds.

Examples:
1y 2m 11s represents a time span of one year, two minutes and 11 seconds.
1234567 represents a time span of 1234567 milliseconds, or 20 minutes, 34 seconds and 567 milliseconds
2d represents a time span of two days.
2m -30s represents 90 seconds.

Version:
$Revision: 1834 $
Author:
Thomas Weber
See Also:
Serialized Form

Nested Class Summary
static class TimeSpan.TimeConstant
          the time constants.
 
Constructor Summary
protected TimeSpan()
          Deprecated. 
  TimeSpan(long millis)
          creates a new TimeSpan with the given amount of milliseconds.
  TimeSpan(java.lang.String ts)
          creates a new TimeSpan from the given time string.
  TimeSpan(TimeSpan source)
          creates a new TimeSpan from the given TimeSpan.
 
Method Summary
 TimeSpan add(long millis)
          adds the given milliseconds to this time span.
 TimeSpan add(TimeSpan s)
          adds a given time span to this time span.
 java.util.Date addTo(java.util.Date d)
          returns a new Date with the time span added to the Date.
 int compareTo(TimeSpan o)
          compare the two time spans.
 boolean equals(java.lang.Object obj)
          checks whether the given object is equal to the time span.
 long getMilliseconds()
          returns the milliseconds.
 java.lang.String getSpan()
          returns the string value.
 int hashCode()
          returns a hash code for this instance.
static long millisFromString(java.lang.String ts)
          parses the time string to a time span in milliseconds.
 void setMilliseconds(long ms)
          sets the milliseconds for the time span.
 void setSpan(java.lang.String s)
          sets the value.
static java.lang.String stringFromMillis(long m)
          returns a time string for the given value in milliseconds.
 TimeSpan subtract(TimeSpan s)
          subtracts a given time span from this time span.
 java.util.Date subtractFrom(java.util.Date d)
          returns a new Date with the time span subtracted from the Date.
 java.lang.String toString()
          returns a string representation of the time span.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TimeSpan

@Deprecated
protected TimeSpan()
Deprecated. 

used by JAXB and HibernateTM.


TimeSpan

public TimeSpan(java.lang.String ts)
creates a new TimeSpan from the given time string.

Parameters:
ts - the time string

TimeSpan

public TimeSpan(long millis)
creates a new TimeSpan with the given amount of milliseconds.

Parameters:
millis - the milliseconds

TimeSpan

public TimeSpan(TimeSpan source)
creates a new TimeSpan from the given TimeSpan.

Parameters:
source - the source time span
Method Detail

millisFromString

public static long millisFromString(java.lang.String ts)
parses the time string to a time span in milliseconds.

Parameters:
ts - the time string
Returns:
the milliseconds
See Also:
stringFromMillis(long)

stringFromMillis

public static java.lang.String stringFromMillis(long m)
returns a time string for the given value in milliseconds.

Parameters:
m - the TimeSpan in milliseconds
Returns:
a time string
See Also:
millisFromString(String)

getMilliseconds

public long getMilliseconds()
returns the milliseconds.

Returns:
the milliseconds

setMilliseconds

public void setMilliseconds(long ms)
sets the milliseconds for the time span.

Parameters:
ms - the time span

getSpan

public java.lang.String getSpan()
returns the string value.

Returns:
the string value

setSpan

public void setSpan(java.lang.String s)
sets the value.

Parameters:
s - the time span string

add

public TimeSpan add(TimeSpan s)
adds a given time span to this time span.

Parameters:
s - the time span to add
Returns:
this time span plus the given time span

add

public TimeSpan add(long millis)
adds the given milliseconds to this time span.

Parameters:
millis - the milliseconds to add
Returns:
this time span plus the given time span

subtract

public TimeSpan subtract(TimeSpan s)
subtracts a given time span from this time span.

Parameters:
s - the time span to add
Returns:
this time span minus the given time span

addTo

public java.util.Date addTo(java.util.Date d)
returns a new Date with the time span added to the Date.

Parameters:
d - the date
Returns:
the computed date

subtractFrom

public java.util.Date subtractFrom(java.util.Date d)
returns a new Date with the time span subtracted from the Date.

Parameters:
d - the date
Returns:
the computed date

toString

public java.lang.String toString()
returns a string representation of the time span.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the time span

hashCode

public int hashCode()
returns a hash code for this instance.

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code
See Also:
Object.hashCode()

equals

public boolean equals(java.lang.Object obj)
checks whether the given object is equal to the time span.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object
Returns:
true, if and only if the two time spans are equal in milliseconds
See Also:
Object.equals(java.lang.Object)

compareTo

public int compareTo(TimeSpan o)
compare the two time spans.

Specified by:
compareTo in interface java.lang.Comparable<TimeSpan>
Parameters:
o - the time span to compare with
Returns:
a negative integer, zero, or a positive integer as this time span is less than, equal to, or greater than the specified time span.
See Also:
Comparable.compareTo(java.lang.Object)