Venice 0.751beta

nz.org.venice.util
Class TradingDate

java.lang.Object
  extended bynz.org.venice.util.TradingDate
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable

public class TradingDate
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Comparable

A replacement date for java.util.Date, java.util.Calendar & java.sql.Date. The main principles of this date class are speed (as fast as possible) and size (as small as possible). It produces a much smaller and faster date class than using the Calendar hierarchy. It also beats java.util.Date 7 * by not using deprecated methods.

Author:
Andrew Leppard

Field Summary
static int BRITISH
          Date format will be in britsh format, e.g.
static int US
          Date format will be in US format, e.g.
 
Constructor Summary
TradingDate()
          Create a new date set to today.
TradingDate(java.util.Calendar date)
          Create a new date from the given java.util.Calendar object.
TradingDate(java.util.Date date)
          Create a new date from the given java.util.Date object.
TradingDate(int year, int month, int day)
          Create a new date from the given year, month and day.
TradingDate(java.lang.String date, int type)
          Create a new date from the given string.
 
Method Summary
 boolean after(java.lang.Object date)
          Tests if this date is after the specified date.
 boolean before(java.lang.Object date)
          Tests if this date is before the specified date.
 java.lang.Object clone()
          Create a clone of this date
 int compareTo(java.lang.Object date)
          Compare the current date to the specified object.
 int compareTo(TradingDate date)
          Compare this date to the specified date.
static java.util.List dateRangeToList(TradingDate startDate, TradingDate endDate)
          Convert a start and end date to a list of all the trading dates inbetween which do not fall on weekends.
 boolean equals(java.lang.Object date)
          Compares this date with the specified object.
 int getDay()
          Return the day of the month.
 int getDayOfWeek()
          Return the day of the week.
 int getDayOfYear()
          Return the day of the year.
 int getDifference(TradingDate date)
          Return the number of trading days between this date and a given date.
 int getMonth()
          Return the month.
 int getYear()
          Return the year.
 int hashCode()
          Create a fast hash code of this date
 boolean isWeekend()
          Return whether the current date falls on a weekend.
static java.lang.String monthToText(int month)
          Convert a month number to its 3 digit name.
 TradingDate next(int days)
          Create a new date which is the specified number of trading days after this date.
 TradingDate previous(int days)
          Create a new date which is the specified number of trading days before this date.
 java.util.Calendar toCalendar()
          Convert this object to a java.util.Calendar.
 java.util.Calendar toCalendar(int year, int month, int day)
          Convert a TradingDate to a java.util.Calendar.
 java.util.Date toDate()
          Convert this object to a java.util.Date.
 java.util.Date toDate(TradingTime time)
          Convert this object and a TradingTime to a java.util.Date.
 java.lang.String toLongString()
          Outputs date in the format - 30 Dec, 2001.
 java.lang.String toShortString()
          Outputs the date in the format - 12/Dec.
 java.lang.String toString()
          Outputs the date in the following format 1/12/2005 or 31/12/2005.
 java.lang.String toString(java.lang.String format)
          Convert date to string in specified format.
static int twoToFourDigitYear(int year)
          Converts a two digit year to four digit year.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

US

public static final int US
Date format will be in US format, e.g. mm/dd/yy, mm/dd/yyyy etc.

See Also:
Constant Field Values

BRITISH

public static final int BRITISH
Date format will be in britsh format, e.g. dd/mm/yy, dd/mm/yyyy etc.

See Also:
Constant Field Values
Constructor Detail

TradingDate

public TradingDate(int year,
                   int month,
                   int day)
Create a new date from the given year, month and day.

Parameters:
year - a four digit year, e.g. 1996
month - the month starting from 1
day - the day starting from 1

TradingDate

public TradingDate(java.util.Calendar date)
Create a new date from the given java.util.Calendar object.

Parameters:
date - calendar date to convert

TradingDate

public TradingDate(java.util.Date date)
Create a new date from the given java.util.Date object.

Parameters:
date - the date to convert

TradingDate

public TradingDate(java.lang.String date,
                   int type)
            throws TradingDateFormatException
Create a new date from the given string. We can parse the following date strings:

YYMMDD
e.g. "010203"
YYYYMMDD
e.g. "20010203"
YYYY-MM-DD
e.g. "2001-02-03"
YYYY/MM/DD
e.g. "2001/02/03"
DD/MM/YY
e.g. "3/2/01"
DD/MM/YYYY
e.g. "3/2/2001"
MM/DD/YY
e.g. "2/3/01"
MM/DD/YYYY
e.g. "2/3/2001"
DD-MM-YY
e.g. "3-2-01"
DD-MM-YYYY
e.g. "3-2-2001"
MM-DD-YY
e.g. "2-3-01"
MM-DD-YYYY
e.g. "2-3-2001"
DD/MON/YY
e.g. "3/Feb/01"
DD/MON/YYYY
e.g. "3/Feb/2001"
DD-MONTH-YY
e.g. "3-February-01"
DD-MONTH-YYYY
e.g. "3-February-2001"

Parameters:
date - the date string to parse
type - either BRITISH or US
Throws:
TradingDateException - if the date couldn't be parsed
TradingDateFormatException

TradingDate

public TradingDate()
Create a new date set to today.

Method Detail

getYear

public int getYear()
Return the year.

Returns:
four digit year

getMonth

public int getMonth()
Return the month.

Returns:
the month starting with 1 for January

getDay

public int getDay()
Return the day of the month.

Returns:
the day of the month starting from 1

getDayOfWeek

public int getDayOfWeek()
Return the day of the week.

Returns:
the day of the week

getDayOfYear

public int getDayOfYear()
Return the day of the year.

Returns:
the day of the year

before

public boolean before(java.lang.Object date)
Tests if this date is before the specified date.

Parameters:
date - the specified date to compare
Returns:
true if the given date is before this one

after

public boolean after(java.lang.Object date)
Tests if this date is after the specified date.

Parameters:
date - the specified date to compare
Returns:
true if the specified date is before this one; false otherwise.

equals

public boolean equals(java.lang.Object date)
Compares this date with the specified object.

Parameters:
date - the specified date to compare
Returns:
true if the specified date is equal; false otherwise.

clone

public java.lang.Object clone()
Create a clone of this date

Returns:
a clone of this date

hashCode

public int hashCode()
Create a fast hash code of this date

Returns:
hash code

previous

public TradingDate previous(int days)
Create a new date which is the specified number of trading days before this date.

Parameters:
days - the number of days to move
Returns:
date which is days before the current one

next

public TradingDate next(int days)
Create a new date which is the specified number of trading days after this date.

Parameters:
days - the number of days to move
Returns:
date which is days after the current one

isWeekend

public boolean isWeekend()
Return whether the current date falls on a weekend.

Returns:
true if the current date is on a weekend.

getDifference

public int getDifference(TradingDate date)
Return the number of trading days between this date and a given date.

Parameters:
date - The date to calculate the difference with.
Returns:
The number of trading days between this date and the given date

compareTo

public int compareTo(java.lang.Object date)
Compare the current date to the specified object.

Specified by:
compareTo in interface java.lang.Comparable
See Also:
compareTo(TradingDate)

compareTo

public int compareTo(TradingDate date)
Compare this date to the specified date.

Parameters:
date - the date to compare
Returns:
the value 0 if the dates are equal; 1 if this date is after the specified date or -1 if this date is before the specified date.

toString

public java.lang.String toString(java.lang.String format)
Convert date to string in specified format. Will convert the date to a string matching the given format. The following substitutions will be made:

d?
Replaced with one or two digit day
dd
Replaced with two digit day
m?
Replaced with one or two digit month
mm
Replaced with two digit month
MMM
Replaced with 3 letter month name
yy
Replaced with two digit year
yyyy
Replaced with four digit year

E.g.:

text = date.toString("d?-m?-yyyy");

Parameters:
format - the format of the string
Returns:
the text string

toString

public java.lang.String toString()
Outputs the date in the following format 1/12/2005 or 31/12/2005.

Returns:
String representation of date.

toShortString

public java.lang.String toShortString()
Outputs the date in the format - 12/Dec.

Returns:
short version of the date string

toLongString

public java.lang.String toLongString()
Outputs date in the format - 30 Dec, 2001.

Returns:
long version of the date string

monthToText

public static java.lang.String monthToText(int month)
Convert a month number to its 3 digit name.

Parameters:
month - the month number
Returns:
the 3 digit month string

toDate

public java.util.Date toDate()
Convert this object to a java.util.Date.

Returns:
java.util.Date

toDate

public java.util.Date toDate(TradingTime time)
Convert this object and a TradingTime to a java.util.Date.

Returns:
java.util.Date

toCalendar

public java.util.Calendar toCalendar()
Convert this object to a java.util.Calendar.

Returns:
java.util.Calendar

toCalendar

public java.util.Calendar toCalendar(int year,
                                     int month,
                                     int day)
Convert a TradingDate to a java.util.Calendar.

Returns:
java.util.Calendar

twoToFourDigitYear

public static int twoToFourDigitYear(int year)
Converts a two digit year to four digit year. The year 0 to 30 are transformed to 2000 to 2030 respecitvely; the years 31 to 99 to 1931 and 1999 respectively.

Parameters:
year - a two digit year
Returns:
a four digit year

dateRangeToList

public static java.util.List dateRangeToList(TradingDate startDate,
                                             TradingDate endDate)
Convert a start and end date to a list of all the trading dates inbetween which do not fall on weekends.

Parameters:
startDate - the start date of the range
endDate - the end date of the range
Returns:
a list of all the trading dates inbetween

Venice 0.751beta