com.jstatcom.ts
Class TSDate
- java.lang.Object
-
- com.jstatcom.ts.TSDate
-
- All Implemented Interfaces:
- java.lang.Comparable
public final class TSDate extends java.lang.Object implements java.lang.ComparableRepresentation of a date for an observation of a time series. It defines amainPeriod, asubPeriodand asubPeriodicity. ThesubPeriodicityquantifies the number of sub periods which constitute one main period. It is also called frequency. A range of twoTSDateobjects is represented by theTSDateRangeclass.This class is immutable.
- See Also:
TSDateRange
-
-
Constructor Summary
Constructors Constructor and Description TSDate()Default constructor creates a standardTSDateobject with string representation 1.TSDate(double newDate, int newSubPeriodicity)Creates aTSDatewith a double coded date and a sub periodicity.TSDate(int mainPeriod, int subPeriod, int subPeriodicity)Creates aTSDateobject from a main period, a sub period, and a sub periodicity.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description TSDateaddPeriods(int n)Adds n periods to a copy of theTSDateobject.intcompareTo(java.lang.Object o)Compares twoTSDateobjects.doubledoubleValue()Returns this date in number form.booleanequals(java.lang.Object o)Overridesequalsto provide logical comparison of two differentTSDateobjects.java.util.DategetDate()Currently only implemented for daily dates, otherwise an Exception is thrown.inthashCode()OverrideshashCode, because equals was overwritten.intmainPeriod()Returns the main period of this date.java.lang.StringprintNice()Gets a nicely formatted string representing this date.java.lang.StringprintShort()Gets a formatted string representing this date that is very short.intsubPeriod()Returns the sub period of this date.intsubPeriodFor(int n)Gets the sub period is reached whennperiods are added to this date.intsubPeriodFormat()Returns the sub period format for the double representation of this date.intsubPeriodicity()Returns the sub periodicity of this date.java.lang.String[]timeAxisStringArray(int t)This method creates a time axis starting from this date as an array of Strings of lengtht, for example{"1960 Q1", "1960 Q2", ..., "1982 Q4"}.java.lang.StringtoString()Returns a string representation of thisTSDate.static TSDatevalueOf(java.lang.String exp)Static initializer that creates aTSDatefrom a string expression.
-
-
-
Constructor Detail
-
TSDate
public TSDate()
Default constructor creates a standardTSDateobject with string representation 1.
-
TSDate
public TSDate(double newDate, int newSubPeriodicity)Creates aTSDatewith a double coded date and a sub periodicity.- Parameters:
newDate- double coded date.newSubPeriodicity- maximum number of sub periods.- Throws:
java.lang.IllegalArgumentException- if subPeriodicity < 1 or subPeriod < 1 or subPeriodicity < subPeriod
-
TSDate
public TSDate(int mainPeriod, int subPeriod, int subPeriodicity)Creates aTSDateobject from a main period, a sub period, and a sub periodicity.- Parameters:
mainPeriod- the main period (left of the decimal point).subPeriod- the sub period ('right' of the decimal point).subPeriodicity- the sub periodicity.- Throws:
java.lang.IllegalArgumentException-if (subPeriodicity < 1 || subPeriod < 1 || subPeriodicity < subPeriod)
-
-
Method Detail
-
addPeriods
public TSDate addPeriods(int n)
Adds n periods to a copy of theTSDateobject.- Parameters:
n- number of periods to be added, can be negative- Returns:
- new
TSDatewithnperiods added
-
compareTo
public int compareTo(java.lang.Object o)
Compares twoTSDateobjects.- Specified by:
compareToin interfacejava.lang.Comparable- Returns:
- 0 if they are equal, otherwise number of periods between the two
dates, which is < 0 if
(TSDate) ois earlier than this - Throws:
java.lang.ClassCastException- if o is not aTSDatejava.lang.IllegalArgumentException- if sub periodicities do not match, because there is no meaningful way to compare the dates
-
doubleValue
public double doubleValue()
Returns this date in number form.- Returns:
- double representation of this
TSDate
-
equals
public boolean equals(java.lang.Object o)
Overridesequalsto provide logical comparison of two differentTSDateobjects.- Overrides:
equalsin classjava.lang.Object- Parameters:
o- object to compare with- Returns:
trueif mainPeriod, subPeriod and subPeriodicity are the same,falseotherwise
-
mainPeriod
public int mainPeriod()
Returns the main period of this date.- Returns:
- the main period (left of the decimal point):
-
subPeriod
public int subPeriod()
Returns the sub period of this date.- Returns:
- sub period, is always within [1,
subPeriodicity]
-
subPeriodFor
public int subPeriodFor(int n)
Gets the sub period is reached whennperiods are added to this date.- Parameters:
n- number of periods to be added, can be negative- Returns:
- the sub period as
int
-
subPeriodFormat
public int subPeriodFormat()
Returns the sub period format for the double representation of this date. This number acts as a place holder for the digits of the sub period. IfsubPeriodicity < 10, this number returns 10, if10 <= subPeriodicity < 100, this number returns 100 and so on. If one multiplies the double coded date with the sub period format, one always gets a value that is equal to an integer, because the decimal digits vanish.- Returns:
- the sub period format
-
subPeriodicity
public int subPeriodicity()
Returns the sub periodicity of this date. ThesubPeriodicityquantifies the number of sub periods which constitute one main period.- Returns:
- the sub periodicity
-
hashCode
public int hashCode()
OverrideshashCode, because equals was overwritten. The algorithm was taken from (Joshua Bloch, "Effective Java", Item 8) .- Overrides:
hashCodein classjava.lang.Object- Returns:
- hash code
-
printNice
public java.lang.String printNice()
Gets a nicely formatted string representing this date. Unlike thetoStringmethod, the resulting string is not necessarily a valid argument for thevalueOfmethod. Especially daily data is printed differently with the weekday and without "(5)" or "(7)".- Returns:
- formatted string
-
printShort
public java.lang.String printShort()
Gets a formatted string representing this date that is very short. Unlike thetoStringmethod, the resulting string is not necessarily a valid argument for thevalueOfmethod. Especially daily data is printed differently with the weekday and without "(5)" or "(7)".- Returns:
- formatted string
-
timeAxisStringArray
public java.lang.String[] timeAxisStringArray(int t)
This method creates a time axis starting from this date as an array of Strings of lengtht, for example{"1960 Q1", "1960 Q2", ..., "1982 Q4"}.- Parameters:
t- int Length of the time axis.- Returns:
- String[] time axis with dates for every time index
- Throws:
java.lang.IllegalArgumentException- if t < 1
-
getDate
public java.util.Date getDate()
Currently only implemented for daily dates, otherwise an Exception is thrown. Assembles ajava.util.Dateobject from this. The returned date always denotes the start of this date.- Returns:
- date
- Throws:
java.lang.IllegalStateException- if this does not represent daily data
-
toString
public java.lang.String toString()
Returns a string representation of thisTSDate. ATSDateobject can be constructed from a string using theTSDateParseror respective theTSDate.valueOfinitializer. Do not use for comparisons, but use equals instead.- Overrides:
toStringin classjava.lang.Object- Returns:
- String representation of this object.
- See Also:
TSDateParser
-
valueOf
public static TSDate valueOf(java.lang.String exp)
Static initializer that creates aTSDatefrom a string expression. The format of the string is based on the following rules:XX x"/"y, for example1960 1/1is recognized for arbitrary sub periodicities with mainPeriod=XX, subPeriod=x, subPeriodicity=yXX ["M","m"]x, for example1960 M1is recognized as mainPeriod=XX, subPeriod=x, subPeriodicity=12 (monthly)XX ["Q","q"]x, for example1960 Q1is recognized as mainPeriod=XX, subPeriod=x, subPeriodicity=4 (quarterly)XX ["I", "II"], for example1960 Iis recognized as mainPeriod=XX, subPeriod=1 or 2, subPeriodicity=2 (half yearly)XX, for example1960is recognized as mainPeriod=XX, subPeriod=1, subPeriodicity=1 (yearly)
Daily Data:
Daily data is a special case. It is always assumed by this class when the periodicity is either 5 (working days) or 7 (all days). It can be specified by using the default date format for the US locale, followed by"(5)"or"(7)", for example:
1/1/73 (5)If a 5 day week is specified and the date falls on a weekend, it is automatically set to the following monday without further notice.
10/12/2004 (7)
- Parameters:
exp- expression to parse to a date- Returns:
- a
TSDateobject created fromexp - Throws:
java.lang.IllegalArgumentException- if object could not be constructed from given string or ifexp == null- See Also:
TSDateParser
-
-
DMelt 3.0 © DataMelt by jWork.ORG