org.silverpeas.date
Class Period

java.lang.Object
  extended by org.silverpeas.date.Period
All Implemented Interfaces:
Serializable, Cloneable, Comparable
Direct Known Subclasses:
DayPeriod, MonthPeriod, WeekPeriod, YearPeriod

public class Period
extends Object
implements Comparable, Serializable, Cloneable

Class representing a period between two dates and offering tools around it.

User: Yohann Chastagnier Date: 19/04/13

See Also:
Serialized Form

Field Summary
static Period UNDEFINED
           
 
Constructor Summary
protected Period(DateTime beginDatable, DateTime endDatable)
          Constructor : Constructs a newly allocated period.
protected Period(DateTime beginDatable, DateTime endDatable, TimeZone timeZone)
          Constructor : Constructs a newly allocated period.
 
Method Summary
static Period check(Period period)
          Checks if the specified period and returns the specified one if defined, or the common UNDEFINED otherwise.
 Period clone()
           
 int compareTo(Object o)
           
 int compareTo(Period period)
          The comparison is at first time executed on the begin dates.
 boolean contains(Date date)
          Checks if the given date is included in the period.
 boolean contains(Period period)
          Checks if the given period is included in the period.
 boolean containsPartOf(Period period)
          Checks if the given period is partially included in the period.
 boolean equals(Object o)
           
protected  String formatPeriodForTests()
           
static Period from(Date beginDate, Date endDate)
          Initialize a period from given dates.
static Period from(Date beginDate, Date endDate, TimeZone timeZone)
          Initialize a period from given dates.
static Period from(Date referenceDate, PeriodType periodType)
          Initialize a period from a date and a type of period.
static Period from(Date referenceDate, PeriodType periodType, String locale)
          Initialize a period from a date and a type of period.
static Period from(DateTime beginDatable, DateTime endDatable)
          Initialize a period from given dates.
static Period from(DateTime referenceDatable, PeriodType periodType)
          Initialize a period from a date and a type of period.
static Period from(DateTime referenceDatable, PeriodType periodType, String locale)
          Initialize a period from a date and a type of period.
static Period from(Date referenceDate, TimeZone timeZone, PeriodType periodType)
          Initialize a period from a date and a type of period.
static Period from(Date referenceDate, TimeZone timeZone, PeriodType periodType, String locale)
          Initialize a period from a date and a type of period.
 DateTime getBeginDatable()
          Gets the begin date of the period represented as a Datable.
 Date getBeginDate()
          Gets the begin date of the period.
 TimeData getCoveredDaysTimeData()
          Computes the number of whole or partial days covered by the period.
 TimeData getElapsedTimeData()
          Computes the elapsed time between the begin and the end dates.
 DateTime getEndDatable()
          Gets the end date of the period represented as a Datable.
 Date getEndDate()
          Gets the end date of the period.
 PeriodType getPeriodType()
          Gets the type of the period.
static Period getPeriodWithUndefinedIfNull(Date beginDate, Date endDate)
          Initialize a period from given dates (undefined dates are taken into account for null ones).
 TimeZone getTimeZone()
          The time zone has no meaning for a date.
 int hashCode()
           
 Period inTimeZone(TimeZone timeZone)
          The time zone for a period has no meaning.
 boolean isBeginDefined()
          This method is a shortcut of isDefined() call on getBeginDatable().
 boolean isBeginNotDefined()
          This method is a shortcut of isNotDefined() call on getBeginDatable().
 boolean isDefined()
          Indicates if one of begin date or end date is defined.
 boolean isEndDefined()
          This method is a shortcut of isDefined() call on getEndDatable().
 boolean isEndNotDefined()
          This method is a shortcut of isNotDefined() call on getEndDatable().
 boolean isLongerThan(Period period)
          Indicates if the period is longer than th given one.
 boolean isNotDefined()
          Indicates the opssite of isDefined().
 boolean isValid()
          Indicates if the period is valid.
 void setDate(Date dateReference, PeriodType periodType)
          Sets the begin and the end dates of the period.
 void setDate(DateTime referenceDatable, PeriodType periodType)
          Sets the begin and the end dates of the period.
 void setDate(Date dateReference, TimeZone timeZone, PeriodType periodType)
          Sets the begin and the end dates of the period.
 void setDates(Date beginDate, Date endDate)
          Sets the begin and the end dates of the period.
 void setDates(Date beginDate, Date endDate, TimeZone timeZone)
          Sets the begin and the end dates of the period.
 void setDates(DateTime beginDatable, DateTime endDatable)
          Sets the begin and the end dates of the period.
protected  void setPeriodType(PeriodType periodType)
          Sets the type of the period.
 String toString()
          For debugging.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

UNDEFINED

public static final Period UNDEFINED
Constructor Detail

Period

protected Period(DateTime beginDatable,
                 DateTime endDatable)
Constructor : Constructs a newly allocated period. TimeZone is set from the begin date of the period.

Parameters:
beginDatable - the date of the beginning of the period.
endDatable - the date of the ending of the period.

Period

protected Period(DateTime beginDatable,
                 DateTime endDatable,
                 TimeZone timeZone)
Constructor : Constructs a newly allocated period.

Parameters:
beginDatable - the date of the beginning of the period.
endDatable - the date of the ending of the period.
timeZone - the time zone to set to this period.
Method Detail

check

public static Period check(Period period)
Checks if the specified period and returns the specified one if defined, or the common UNDEFINED otherwise.

Parameters:
period -
Returns:
the specified period is defined, the common UNDEFINED otherwise.

getPeriodWithUndefinedIfNull

public static Period getPeriodWithUndefinedIfNull(Date beginDate,
                                                  Date endDate)
Initialize a period from given dates (undefined dates are taken into account for null ones). The treatment is trying to identify the type of the period (PeriodType).

Parameters:
beginDate - the date of the beginning of the period.
endDate - the date of the ending of the period.
Returns:

from

public static Period from(Date beginDate,
                          Date endDate)
Initialize a period from given dates. The treatment is trying to identify the type of the period (PeriodType).

Parameters:
beginDate - the date of the beginning of the period.
endDate - the date of the ending of the period.
Returns:

from

public static Period from(Date beginDate,
                          Date endDate,
                          TimeZone timeZone)
Initialize a period from given dates. The treatment is trying to identify the type of the period (PeriodType).

Parameters:
beginDate - the date of the beginning of the period.
endDate - the date of the ending of the period.
timeZone - the time zone to set to this period.
Returns:

from

public static Period from(DateTime beginDatable,
                          DateTime endDatable)
Initialize a period from given dates. The treatment is trying to identify the type of the period (PeriodType).

Parameters:
beginDatable - the date of the beginning of the period.
endDatable - the date of the ending of the period.
Returns:

from

public static Period from(Date referenceDate,
                          PeriodType periodType)
Initialize a period from a date and a type of period.

Parameters:
referenceDate -
periodType -
Returns:

from

public static Period from(Date referenceDate,
                          TimeZone timeZone,
                          PeriodType periodType)
Initialize a period from a date and a type of period.

Parameters:
referenceDate -
timeZone -
periodType -
Returns:

from

public static Period from(DateTime referenceDatable,
                          PeriodType periodType)
Initialize a period from a date and a type of period.

Parameters:
referenceDatable -
periodType -
Returns:

from

public static Period from(Date referenceDate,
                          PeriodType periodType,
                          String locale)
Initialize a period from a date and a type of period.

Parameters:
referenceDate -
periodType -
locale -
Returns:

from

public static Period from(Date referenceDate,
                          TimeZone timeZone,
                          PeriodType periodType,
                          String locale)
Initialize a period from a date and a type of period.

Parameters:
referenceDate -
timeZone -
periodType -
locale -
Returns:

from

public static Period from(DateTime referenceDatable,
                          PeriodType periodType,
                          String locale)
Initialize a period from a date and a type of period.

Parameters:
referenceDatable -
periodType -
locale -
Returns:

isDefined

public boolean isDefined()
Indicates if one of begin date or end date is defined.

Returns:
true if period is defined, false otherwise.

isNotDefined

public boolean isNotDefined()
Indicates the opssite of isDefined().

Returns:
true if period is not defined, false otherwise.

inTimeZone

public Period inTimeZone(TimeZone timeZone)
The time zone for a period has no meaning.

Parameters:
timeZone - the time zone to set to this period.

getTimeZone

public TimeZone getTimeZone()
The time zone has no meaning for a date.

Returns:
the time zone in which this period is set.

getPeriodType

public PeriodType getPeriodType()
Gets the type of the period.

Returns:

setPeriodType

protected void setPeriodType(PeriodType periodType)
Sets the type of the period.

Parameters:
periodType -

getBeginDate

public Date getBeginDate()
Gets the begin date of the period. It represents the begin date of the event if no recurrence exists, or the begin date of the first occurence of the event if a recurrence exists.

Returns:

getEndDate

public Date getEndDate()
Gets the end date of the period.

Returns:

getBeginDatable

public DateTime getBeginDatable()
Gets the begin date of the period represented as a Datable. (see getBeginDate() for more details).

Returns:

isBeginDefined

public boolean isBeginDefined()
This method is a shortcut of isDefined() call on getBeginDatable().

Returns:
true if getBeginDatable() returns defined date, false otherwise.

isBeginNotDefined

public boolean isBeginNotDefined()
This method is a shortcut of isNotDefined() call on getBeginDatable().

Returns:
true if getBeginDatable() returns a not defined date, false otherwise.

getEndDatable

public DateTime getEndDatable()
Gets the end date of the period represented as a Datable. (see getEndDate() for more details).

Returns:

isEndDefined

public boolean isEndDefined()
This method is a shortcut of isDefined() call on getEndDatable().

Returns:
true if getEndDatable() returns defined date, false otherwise.

isEndNotDefined

public boolean isEndNotDefined()
This method is a shortcut of isNotDefined() call on getEndDatable().

Returns:
true if getEndDatable() returns a not defined date, false otherwise.

setDate

public void setDate(Date dateReference,
                    PeriodType periodType)
Sets the begin and the end dates of the period.

Parameters:
dateReference -

setDate

public void setDate(Date dateReference,
                    TimeZone timeZone,
                    PeriodType periodType)
Sets the begin and the end dates of the period.

Parameters:
dateReference -
timeZone -

setDate

public void setDate(DateTime referenceDatable,
                    PeriodType periodType)
Sets the begin and the end dates of the period.

Parameters:
referenceDatable -

setDates

public void setDates(Date beginDate,
                     Date endDate)
Sets the begin and the end dates of the period.

Parameters:
beginDate -

setDates

public void setDates(Date beginDate,
                     Date endDate,
                     TimeZone timeZone)
Sets the begin and the end dates of the period.

Parameters:
beginDate -
timeZone -

setDates

public void setDates(DateTime beginDatable,
                     DateTime endDatable)
Sets the begin and the end dates of the period.

Parameters:
beginDatable -

getElapsedTimeData

public TimeData getElapsedTimeData()
Computes the elapsed time between the begin and the end dates.

Returns:
the elapsed time computed represented by TimeData. To retrieve informations from this returned object :
   // Gets the elpased time in milliseconds
period.getElapsedTimeData().getTime();

// Gets the elpased time in seconds
period.getElapsedTimeData().getTimeConverted(TimeUnit.SEC);

// Gets the elpased time in years
period.getElapsedTimeData().getTimeConverted(TimeUnit.YEAR);

...
// Gets the elpased time in the best unit value
period.getElapsedTimeData().getBestValue();

getCoveredDaysTimeData

public TimeData getCoveredDaysTimeData()
Computes the number of whole or partial days covered by the period.

Returns:
the number of whole or partial days covered by the period. represented by TimeData. To retrieve informations from this returned object :
   // Gets the number of days (in milliseconds)
period.getCoveredDaysTimeData().getTime();

// Gets the number of days (in days)
period.getCoveredDaysTimeData().getTimeConverted(TimeUnit.DAY);

...
// Gets the number of days (in best unit value)
period.getCoveredDaysTimeData().getBestValue();

compareTo

public int compareTo(Object o)
Specified by:
compareTo in interface Comparable

compareTo

public int compareTo(Period period)
The comparison is at first time executed on the begin dates. If they are equals, then it is executed on the end dates.

Parameters:
period -
Returns:

isLongerThan

public boolean isLongerThan(Period period)
Indicates if the period is longer than th given one.

Parameters:
period -
Returns:

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

isValid

public boolean isValid()
Indicates if the period is valid.

Returns:

toString

public String toString()
For debugging.

Overrides:
toString in class Object
Returns:

formatPeriodForTests

protected String formatPeriodForTests()

contains

public boolean contains(Date date)
Checks if the given date is included in the period.

Parameters:
date - a date
Returns:
true if the given date is included in the period.

contains

public boolean contains(Period period)
Checks if the given period is included in the period.

Parameters:
period - a period
Returns:
true if the given period is included in the period.

containsPartOf

public boolean containsPartOf(Period period)
Checks if the given period is partially included in the period.

Parameters:
period - Une date
Returns:
true if the given period is partially included in the period.

clone

public Period clone()
Overrides:
clone in class Object


Copyright © 2016 Silverpeas. All Rights Reserved.