System
Class TimeSpan

java.lang.Object
  extended by System.TimeSpan
All Implemented Interfaces:
IComparable<TimeSpan>, IEquatable<TimeSpan>

public class TimeSpan
extends java.lang.Object
implements IComparable<TimeSpan>, IEquatable<TimeSpan>

Represents a time interval.

Author:
Halofreak1990

Field Summary
static TimeSpan MaxValue
          Represents the maximum TimeSpan value.
static TimeSpan MinValue
          Represents the minimum TimeSpan value.
 long TicksPerDay
          Represents the number of ticks in 1 day.
 long TicksPerHour
          Represents the number of ticks in 1 hour.
 long TicksPerMillisecond
          Represents the number of ticks in 1 millisecond.
 long TicksPerMinute
          Represents the number of ticks in 1 minute.
 long TicksPerSecond
          Represents the number of ticks in 1 second.
static TimeSpan Zero
          Represents the zero TimeSpan value.
 
Constructor Summary
TimeSpan(int hours, int minutes, int seconds)
          Initializes a new TimeSpan to a specified number of hours, minutes, and seconds.
TimeSpan(int days, int hours, int minutes, int seconds)
          Initializes a new TimeSpan to a specified number of days, hours, minutes, and seconds.
TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
          Initializes a new TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds.
TimeSpan(long ticks)
          Initializes a new TimeSpan to the specified number of ticks.
 
Method Summary
 TimeSpan Add(TimeSpan ts)
          Adds the specified TimeSpan to this instance.
static int Compare(TimeSpan t1, TimeSpan t2)
          Compares two TimeSpan values and returns an integer that indicates their relationship.
 int CompareTo(TimeSpan value)
          Compares this instance to a specified TimeSpan object and returns an indication of their relative values.
 TimeSpan Duration()
          Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.
 boolean equals(java.lang.Object value)
          Returns a value indicating whether this instance is equal to a specified object.
 boolean Equals(TimeSpan obj)
          Returns a value indicating whether this instance is equal to a specified TimeSpan object.
static boolean Equals(TimeSpan t1, TimeSpan t2)
          Returns a value indicating whether two specified instances of TimeSpan are equal.
static TimeSpan FromDays(double value)
          Returns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond.
static TimeSpan FromHours(double value)
          Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
static TimeSpan FromMilliseconds(double value)
          Returns a TimeSpan that represents a specified number of milliseconds.
static TimeSpan FromMinutes(double value)
          Returns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond.
static TimeSpan FromSeconds(double value)
          Returns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond.
static TimeSpan FromTicks(long value)
          Returns a TimeSpan that represents a specified time, where the specification is in units of ticks.
 int getDays()
          Gets the number of whole days represented by the current TimeSpan structure.
 int getHours()
          Gets the number of whole hours represented by the current TimeSpan structure.
 int getMilliseconds()
          Gets the number of whole milliseconds represented by the current TimeSpan structure.
 int getMinutes()
          Gets the number of whole minutes represented by the current TimeSpan structure.
 int getSeconds()
          Gets the number of whole seconds represented by the current TimeSpan structure.
 long getTicks()
          Gets the number of ticks that represent the value of the current TimeSpan structure.
 double getTotalDays()
          Gets the value of the current TimeSpan structure expressed in whole and fractional days.
 double getTotalHours()
          Gets the value of the current TimeSpan structure expressed in whole and fractional hours.
 double getTotalMilliseconds()
          Gets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.
 double getTotalMinutes()
          Gets the value of the current TimeSpan structure expressed in whole and fractional minutes.
 double getTotalSeconds()
          Gets the value of the current TimeSpan structure expressed in whole and fractional seconds.
 int hashCode()
          Returns a hash code for this instance.
 TimeSpan Negate()
          Returns a TimeSpan whose value is the negated value of this instance.
static TimeSpan Parse(java.lang.String s)
          Constructs a new TimeSpan object from a time interval specified in a string.
 TimeSpan Subtract(TimeSpan ts)
          Subtracts the specified TimeSpan from this instance.
 java.lang.String toString()
          Returns the string representation of the value of this instance.
static boolean TryParse(java.lang.String s, TimeSpan result)
          Constructs a new TimeSpan object from a time interval specified in a string.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

TicksPerMillisecond

public final long TicksPerMillisecond
Represents the number of ticks in 1 millisecond. This field is constant.

See Also:
Constant Field Values

TicksPerSecond

public final long TicksPerSecond
Represents the number of ticks in 1 second.

See Also:
Constant Field Values

TicksPerMinute

public final long TicksPerMinute
Represents the number of ticks in 1 minute. This field is constant.

See Also:
Constant Field Values

TicksPerHour

public final long TicksPerHour
Represents the number of ticks in 1 hour. This field is constant.

See Also:
Constant Field Values

TicksPerDay

public final long TicksPerDay
Represents the number of ticks in 1 day. This field is constant.

See Also:
Constant Field Values

Zero

public static final TimeSpan Zero
Represents the zero TimeSpan value. This field is read-only.


MaxValue

public static final TimeSpan MaxValue
Represents the maximum TimeSpan value. This field is read-only.


MinValue

public static final TimeSpan MinValue
Represents the minimum TimeSpan value. This field is read-only.

Constructor Detail

TimeSpan

public TimeSpan(long ticks)
Initializes a new TimeSpan to the specified number of ticks.

Parameters:
ticks - A time period expressed in 100-nanosecond units.

TimeSpan

public TimeSpan(int hours,
                int minutes,
                int seconds)
Initializes a new TimeSpan to a specified number of hours, minutes, and seconds.

Parameters:
hours - Number of hours.
minutes - Number of minutes.
seconds - Number of seconds.
Throws:
ArgumentOutOfRangeException - The parameters specify a TimeSpan value less than MinValue or greater than MaxValue.

TimeSpan

public TimeSpan(int days,
                int hours,
                int minutes,
                int seconds)
Initializes a new TimeSpan to a specified number of days, hours, minutes, and seconds.

Parameters:
days - Number of days.
hours - Number of hours.
minutes - Number of minutes.
seconds - Number of seconds.
Throws:
ArgumentOutOfRangeException - The parameters specify a TimeSpan value less than MinValue or greater than MaxValue.

TimeSpan

public TimeSpan(int days,
                int hours,
                int minutes,
                int seconds,
                int milliseconds)
Initializes a new TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds.

Parameters:
days - Number of days.
hours - Number of hours.
minutes - Number of minutes.
seconds - Number of seconds.
milliseconds - Number of milliseconds.
Throws:
ArgumentOutOfRangeException - The parameters specify a TimeSpan value less than MinValue or greater than MaxValue.
Method Detail

getDays

public int getDays()
Gets the number of whole days represented by the current TimeSpan structure.

Returns:
The day component of this instance. The return value can be positive or negative.

getHours

public int getHours()
Gets the number of whole hours represented by the current TimeSpan structure.

Returns:
The hour component of the current TimeSpan structure. The return value ranges from -23 through 23.

getMilliseconds

public int getMilliseconds()
Gets the number of whole milliseconds represented by the current TimeSpan structure.

Returns:
The millisecond component of the current TimeSpan structure. The return value ranges from -999 through 999.

getMinutes

public int getMinutes()
Gets the number of whole minutes represented by the current TimeSpan structure.

Returns:
The minute component of the current TimeSpan structure. The return value ranges from -59 through 59.

getSeconds

public int getSeconds()
Gets the number of whole seconds represented by the current TimeSpan structure.

Returns:
The second component of the current TimeSpan structure. The return value ranges from -59 through 59.

getTicks

public long getTicks()
Gets the number of ticks that represent the value of the current TimeSpan structure.

Returns:
The number of ticks contained in this instance.

getTotalDays

public double getTotalDays()
Gets the value of the current TimeSpan structure expressed in whole and fractional days.

Returns:
The total number of days represented by this instance.

getTotalHours

public double getTotalHours()
Gets the value of the current TimeSpan structure expressed in whole and fractional hours.

Returns:
The total number of hours represented by this instance.

getTotalMilliseconds

public double getTotalMilliseconds()
Gets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.

Returns:
The total number of milliseconds represented by this instance.

getTotalMinutes

public double getTotalMinutes()
Gets the value of the current TimeSpan structure expressed in whole and fractional minutes.

Returns:
The total number of minutes represented by this instance.

getTotalSeconds

public double getTotalSeconds()
Gets the value of the current TimeSpan structure expressed in whole and fractional seconds.

Returns:
The total number of seconds represented by this instance.

Add

public TimeSpan Add(TimeSpan ts)
Adds the specified TimeSpan to this instance.

Parameters:
ts - A TimeSpan.
Returns:
A TimeSpan that represents the value of this instance plus the value of ts.
Throws:
OverflowException - The resulting TimeSpan is less than MinValue or greater than MaxValue.

Compare

public static int Compare(TimeSpan t1,
                          TimeSpan t2)
Compares two TimeSpan values and returns an integer that indicates their relationship.

Parameters:
t1 - A TimeSpan.
t2 - A TimeSpan.
Returns:
Value Condition -1 t1 is less than t20 t1 is equal to t21 t1 is greater than t2

CompareTo

public int CompareTo(TimeSpan value)
Compares this instance to a specified TimeSpan object and returns an indication of their relative values.

Specified by:
CompareTo in interface IComparable<TimeSpan>
Parameters:
value - A TimeSpan object to compare to this instance.
Returns:
A signed number indicating the relative values of this instance and value.Value Description A negative integer This instance is less than value. Zero This instance is equal to value. A positive integer This instance is greater than value.

Duration

public TimeSpan Duration()
Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.

Returns:
A new TimeSpan whose value is the absolute value of the current TimeSpan object.
Throws:
OverflowException - The value of this instance is MinValue.

equals

public boolean equals(java.lang.Object value)
Returns a value indicating whether this instance is equal to a specified object.

Overrides:
equals in class java.lang.Object
Parameters:
value - An object to compare with this instance.
Returns:
true if value is a TimeSpan object that represents the same time interval as the current TimeSpan structure; otherwise, false.

Equals

public boolean Equals(TimeSpan obj)
Returns a value indicating whether this instance is equal to a specified TimeSpan object.

Specified by:
Equals in interface IEquatable<TimeSpan>
Parameters:
obj - An TimeSpan object to compare with this instance.
Returns:
true if obj represents the same time interval as this instance; otherwise, false.

Equals

public static boolean Equals(TimeSpan t1,
                             TimeSpan t2)
Returns a value indicating whether two specified instances of TimeSpan are equal.

Parameters:
t1 - A TimeSpan.
t2 - A TimeSpan.
Returns:
true if the values of t1 and t2 are equal; otherwise, false.

FromDays

public static TimeSpan FromDays(double value)
Returns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond.

Parameters:
value - A number of days, accurate to the nearest millisecond.
Returns:
A TimeSpan that represents value.
Throws:
ArgumentException - value is equal to NaN.
OverflowException - value is less than MinValue or greater than MaxValue.

FromHours

public static TimeSpan FromHours(double value)
Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.

Parameters:
value - A number of hours accurate to the nearest millisecond.
Returns:
A TimeSpan that represents value.
Throws:
ArgumentException - value is equal to NaN.
OverflowException - value is less than MinValue or greater than MaxValue.

FromMilliseconds

public static TimeSpan FromMilliseconds(double value)
Returns a TimeSpan that represents a specified number of milliseconds.

Parameters:
value - A number of milliseconds.
Returns:
A TimeSpan that represents value.
Throws:
ArgumentException - value is equal to NaN.
OverflowException - value is less than MinValue or greater than MaxValue.

FromMinutes

public static TimeSpan FromMinutes(double value)
Returns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond.

Parameters:
value - A number of minutes, accurate to the nearest millisecond.
Returns:
A TimeSpan that represents value.
Throws:
ArgumentException - value is equal to NaN.
OverflowException - value is less than MinValue or greater than MaxValue.

FromSeconds

public static TimeSpan FromSeconds(double value)
Returns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond.

Parameters:
value - A number of seconds, accurate to the nearest millisecond.
Returns:
A TimeSpan that represents value.
Throws:
ArgumentException - value is equal to NaN.
OverflowException - value is less than MinValue or greater than MaxValue.

FromTicks

public static TimeSpan FromTicks(long value)
Returns a TimeSpan that represents a specified time, where the specification is in units of ticks.

Parameters:
value - A number of ticks that represent a time.
Returns:
A TimeSpan with a value of value.

hashCode

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

Overrides:
hashCode in class java.lang.Object
Returns:
A 32-bit signed integer hash code.

Negate

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

Returns:
The same numeric value as this instance, but with the opposite sign.
Throws:
OverflowException - The negated value of this instance cannot be represented by a TimeSpan; that is, the value of this instance is MinValue.

Parse

public static TimeSpan Parse(java.lang.String s)
Constructs a new TimeSpan object from a time interval specified in a string.

Parameters:
s - A string that specifies a time interval.
Returns:
A TimeSpan that corresponds to s.
Throws:
FormatException - s has an invalid format.
ArgumentNullException - s is null.
OverflowException - s represents a number less than MinValue or greater than MaxValue.-or- At least one of the days, hours, minutes, or seconds components is outside its valid range.

Subtract

public TimeSpan Subtract(TimeSpan ts)
Subtracts the specified TimeSpan from this instance.

Parameters:
ts - A TimeSpan.
Returns:
A TimeSpan whose value is the result of the value of this instance minus the value of ts.
Throws:
OverflowException - The return value is less than MinValue or greater than MaxValue.

toString

public java.lang.String toString()
Returns the string representation of the value of this instance.

Overrides:
toString in class java.lang.Object
Returns:
A string that represents the value of this instance. The return value is of the form: [-][d.]hh:mm:ss[.ff] Items in square brackets ([ and ]) are optional, colons and periods (: and.) are literal characters; and the other items are as follows.Item Description "-" optional minus sign indicating a negative time "d" optional days "hh" hours, ranging from 0 to 23 "mm" minutes, ranging from 0 to 59 "ss" seconds, ranging from 0 to 59 "ff" optional fractional seconds, from 1 to 7 decimal digits For more information about comparing the string representation of TimeSpan and Oracle data types, see article Q324577, "System.TimeSpan Does Not Match Oracle 9i INTERVAL DAY TO SECOND Data Type," in the Microsoft Knowledge Base at http://support.microsoft.com.

TryParse

public static boolean TryParse(java.lang.String s,
                               TimeSpan result)
Constructs a new TimeSpan object from a time interval specified in a string. Parameters specify the time interval and the variable where the new TimeSpan object is returned.

Parameters:
s - A string that specifies a time interval.
result - When this method returns, contains an object that represents the time interval specified by s, or Zero if the conversion failed. This parameter is passed uninitialized.
Returns:
true if s was converted successfully; otherwise, false. This operation returns false if the s parameter is null, has an invalid format,represents a time interval less than MinValue or greater than MaxValue, or has at least one days, hours, minutes, or seconds component outside its valid range.