Joda Time API

org.joda.time
Class TimePeriod

java.lang.Object
  |
  +--org.joda.time.TimePeriod
All Implemented Interfaces:
java.lang.Comparable, ReadableTimePeriod, java.io.Serializable

public final class TimePeriod
extends java.lang.Object
implements ReadableTimePeriod, java.io.Serializable

TimePeriod is the standard implementation of an immutable time period class. It stores the period by the start date (when used) as a ReadableInstant object and the period length as milliseconds.

Author:
Sean Geoghegan, Stephen Colebourne
See Also:
Serialized Form

Constructor Summary
TimePeriod(long period)
          Creates new TimePeriod.
TimePeriod(ReadableInstant start, ReadableInstant end)
          Creates new TimePeriod This TimePeriod will have a defined start date and a defined end date.
TimePeriod(ReadableInstant start, ReadableTimePeriod dur)
          Creates new TimePeriod.
TimePeriod(ReadableTimePeriod dur, ReadableInstant end)
          Creates new TimePeriod.
TimePeriod(java.lang.String period)
          Creates new TimePeriod.
 
Method Summary
 java.lang.Object clone()
          Public clone
 int compareTo(java.lang.Object obj)
          Compare for natural order.
 boolean equals(java.lang.Object obj)
          Compare for equality.
 java.lang.String format(java.text.Format formatter)
          Format this object using a formatter object.
 int getDays()
          Get the days field of the time period
 Instant getEndInstant()
          Gets the end of this time period as an Instant.
 int getHours()
          Get the hours field of the time period
 long getMillis()
          Gets the length of this time period in milliseconds.
 int getMinutes()
          Get the minutes field of the time period
 int getMonths()
          Get the months field of the time period
 int getSeconds()
          Get the seconds field of the time period
 Instant getStartInstant()
          Gets the start of this time period as an Instant.
protected  long getTimePeriodMillisFromObject(java.lang.Object object)
          Convert an object into a time period.
 int getTotalDays()
          Get the total days field of the time period
 int getTotalHours()
          Get the total hours field of the time period
 int getTotalMinutes()
          Get the total minutes field of the time period
 int getTotalMonths()
          Get the total months field of the time period
 int getTotalSeconds()
          Get the total seconds field of the time period
 int getTotalWeeks()
          Get the total weeks field of the time period
 int getYears()
          Get the years field of the time period
 int hashCode()
          Hashcode compatable with equals method.
 boolean isLongerThan(java.lang.Object obj)
          Is this period longer than the specified period.
 boolean isShorterThan(java.lang.Object obj)
          Is this period shorter than the specified period.
 java.lang.String toString()
          Output a string in ISO8601 time period format.
 TimePeriod toTimePeriod()
          Get the object as a TimePeriod
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TimePeriod

public TimePeriod(long period)
Creates new TimePeriod. This time period will have no defined start or end date.

Parameters:
period - the length of the period in milliseconds.

TimePeriod

public TimePeriod(ReadableInstant start,
                  ReadableInstant end)
Creates new TimePeriod This TimePeriod will have a defined start date and a defined end date.

Parameters:
start - the Start date of this time period
end - the End date of this time period

TimePeriod

public TimePeriod(ReadableInstant start,
                  ReadableTimePeriod dur)
Creates new TimePeriod. This TimePeriod will have a defined start date and a duration.

Parameters:
start - the Start date of the time period
dur - the duration of the time period

TimePeriod

public TimePeriod(ReadableTimePeriod dur,
                  ReadableInstant end)
Creates new TimePeriod. This TimePeriod will have a defined end date and a duration.

Parameters:
dur - the duration of the time period
end - the end of the time period

TimePeriod

public TimePeriod(java.lang.String period)
           throws java.text.ParseException
Creates new TimePeriod. TimePeriod is created from an ISO formatted TimePeriod string. Only accepts strings in the ISO_STANDARD_BASIC_FORMAT pattern.

Parameters:
period - an ISO time period string
Method Detail

clone

public java.lang.Object clone()
Public clone

Overrides:
clone in class java.lang.Object
Returns:
clone of the original object,

getTimePeriodMillisFromObject

protected long getTimePeriodMillisFromObject(java.lang.Object object)
Convert an object into a time period.

Parameters:
object - ReadableTimePeriod, Long or String
Throws:
java.lang.IllegalArgumentException - if the object is null, or the string invalid
java.lang.ClassCastException - if the object type is not supported

format

public java.lang.String format(java.text.Format formatter)
Format this object using a formatter object.

Returns:
formatted string

getMillis

public long getMillis()
Description copied from interface: ReadableTimePeriod
Gets the length of this time period in milliseconds.

Specified by:
getMillis in interface ReadableTimePeriod
Returns:
the length of the time period as milliseconds.
See Also:
ReadableTimePeriod.getMillis()

getStartInstant

public Instant getStartInstant()
Description copied from interface: ReadableTimePeriod
Gets the start of this time period as an Instant.

Specified by:
getStartInstant in interface ReadableTimePeriod
Returns:
the start of the time period.
See Also:
ReadableTimePeriod.getStartInstant()

getEndInstant

public Instant getEndInstant()
Description copied from interface: ReadableTimePeriod
Gets the end of this time period as an Instant.

Specified by:
getEndInstant in interface ReadableTimePeriod
Returns:
the end of the time period.
See Also:
ReadableTimePeriod.getEndInstant()

toTimePeriod

public TimePeriod toTimePeriod()
Get the object as a TimePeriod

Specified by:
toTimePeriod in interface ReadableTimePeriod
Returns:
this

getTotalSeconds

public int getTotalSeconds()
Get the total seconds field of the time period


getTotalMinutes

public int getTotalMinutes()
Get the total minutes field of the time period


getTotalHours

public int getTotalHours()
Get the total hours field of the time period


getTotalDays

public int getTotalDays()
Get the total days field of the time period


getTotalMonths

public int getTotalMonths()
Get the total months field of the time period


getTotalWeeks

public int getTotalWeeks()
Get the total weeks field of the time period


getSeconds

public int getSeconds()
Get the seconds field of the time period


getMinutes

public int getMinutes()
Get the minutes field of the time period


getHours

public int getHours()
Get the hours field of the time period


getDays

public int getDays()
Get the days field of the time period


getMonths

public int getMonths()
Get the months field of the time period


getYears

public int getYears()
Get the years field of the time period


equals

public boolean equals(java.lang.Object obj)
Compare for equality. For two TimePeriods to be equal they must be of the same duration and if a start or end date is defined they too must be equal.

Overrides:
equals in class java.lang.Object
Returns:
true if equal

hashCode

public int hashCode()
Hashcode compatable with equals method.

Overrides:
hashCode in class java.lang.Object
Returns:
suitable hashcode

compareTo

public int compareTo(java.lang.Object obj)
Compare for natural order.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
obj - the object to compare to
Returns:
0 if equal, -1 if this is less, + 1 if greater
Throws:
java.lang.IllegalArgumentException - if the object is null, or the string invalid
java.lang.ClassCastException - if the object type is not supported

isLongerThan

public boolean isLongerThan(java.lang.Object obj)
Is this period longer than the specified period.

Specified by:
isLongerThan in interface ReadableTimePeriod
Parameters:
obj - the period to compare to
Returns:
true if this is longer
Throws:
java.lang.IllegalArgumentException - if the object is null, or the string invalid
java.lang.ClassCastException - if the object type is not supported

isShorterThan

public boolean isShorterThan(java.lang.Object obj)
Is this period shorter than the specified period.

Specified by:
isShorterThan in interface ReadableTimePeriod
Parameters:
obj - the period to compare to
Returns:
true if this is shorter
Throws:
java.lang.IllegalArgumentException - if the object is null, or the string invalid
java.lang.ClassCastException - if the object type is not supported

toString

public java.lang.String toString()
Output a string in ISO8601 time period format.

Overrides:
toString in class java.lang.Object
Returns:
re-parsable string

Joda Time API

Copyright © 2001-2003 Stephen Colebourne. All Rights Reserved.