becker.util
Class DateTime

java.lang.Object
  extended by becker.util.DateTime
All Implemented Interfaces:
Cloneable, Comparable

public class DateTime
extends Object
implements Cloneable, Comparable

A class to represent an instant in time that is simpler to use than either Date or GregorianCalendar.

Author:
Byron Weber Becker

Field Summary
static int DATE_AND_TIME
          Cause format(...) to return the date and time.
static int DATE_ONLY
          Cause format(...) to return the date only.
static int LONG
          Cause format(...) to use a long format.
static int MEDIUM
          Cause format(...) to use a medium-length format.
static int SHORT
          Cause format(...) to use a short format.
static int TIME_ONLY
          Cause format(...) to return the time only.
 
Constructor Summary
DateTime()
          Construct a new date object representing the current instant in time.
DateTime(DateTime dt)
          Construct a new DateTime object that represents the same date as dt.
DateTime(int year, int month, int day)
          Construct a new date object representing the given year, month, and day.
DateTime(int year, int month, int day, int hours, int minutes, int seconds)
          Construct a new date object representing the given year, month, and day.
DateTime(Scanner in)
          Read a date from in, leaving the file cursor immediately after the date.
 
Method Summary
 void addDays(int howMany)
          Add (or subtract, if negative) days from this DateTime.
 void addHours(int howMany)
          Add (or subtract, if negative) hours from this DateTime.
 void addMinutes(int howMany)
          Add (or subtract, if negative) minutes from this DateTime.
 void addMonths(int howMany)
          Add (or subtract, if negative) months from this DateTime.
 void addSeconds(int howMany)
          Add (or subtract, if negative) seconds from this DateTime.
 void addYears(int howMany)
          Add (or subtract, if negative) years from this DateTime.
 Object clone()
          Clone this Date/Time object to produce another one, just like it.
 int compareTo(Object dt)
          Compare two DateTime objects.
 int daysUntil(DateTime another)
          Calculate the number of days until another date.
 boolean equals(Object other)
          Determine whether this Date/Time is equal to another.
 String format()
          Format the date and/or time as a string according to the current settings established by setFormatInclude and setFormatLength.
 int getDay()
          Get the day.
 int getHour()
          Get the hour.
 int getMinute()
          Get the minute.
 int getMonth()
          Get the month.
static DateTime getNowInstance()
          Get an instance representing the current date and time.
 int getSecond()
          Get the second.
 Date getTime()
          Return an object that can be passed to an instance of DateFormat.
static DateTime getTodayInstance()
          Get an instance representing today (time fields set to midnight).
 int getYear()
          Get the year.
 int hashCode()
          Returns a hash code for this object for use in HashSet and related collections.
 boolean isAfter(DateTime other)
          Determine whether this Date/Time is after another.
 boolean isBefore(DateTime other)
          Determine whether this Date/Time is before another.
 void setDay(int day)
          Set the day for this to the given day
 void setFormatInclude(int formatInclude)
          Set the data to include in the string returned by format().
 void setFormatLength(int formatLength)
          Set the length of the string returned by format().
 void setHour(int hour)
          Set the hour for this to the given hour
 void setMinute(int minute)
          Set the minute for this to the given minute
 void setMonth(int month)
          Set the month for this to the given month
 void setSecond(int second)
          Set the second for this to the given second
 void setTime(int hour, int min, int sec)
          Set the time for this to the given hour, minute and second.
 void setYear(int year)
          Set the year for this to the given year
 String toString()
          Represent this date (and time, if the time is not midnight) as a string.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SHORT

public static final int SHORT
Cause format(...) to use a short format.

See Also:
Constant Field Values

MEDIUM

public static final int MEDIUM
Cause format(...) to use a medium-length format.

See Also:
Constant Field Values

LONG

public static final int LONG
Cause format(...) to use a long format.

See Also:
Constant Field Values

DATE_ONLY

public static final int DATE_ONLY
Cause format(...) to return the date only.

See Also:
Constant Field Values

TIME_ONLY

public static final int TIME_ONLY
Cause format(...) to return the time only.

See Also:
Constant Field Values

DATE_AND_TIME

public static final int DATE_AND_TIME
Cause format(...) to return the date and time.

See Also:
Constant Field Values
Constructor Detail

DateTime

public DateTime()
Construct a new date object representing the current instant in time.


DateTime

public DateTime(int year,
                int month,
                int day)
Construct a new date object representing the given year, month, and day.

Parameters:
year - The year {e.g. 2003}
month - The month in the range 1..12
day - The day in the range 1..31

DateTime

public DateTime(int year,
                int month,
                int day,
                int hours,
                int minutes,
                int seconds)
Construct a new date object representing the given year, month, and day.

Parameters:
year - The year {e.g. 2003}
month - The month in the range 1..12
day - The day in the range 1..31
hours - The hour in the range 0..23
minutes - The minutes in the range 0..59
seconds - The seconds in the range 0..59

DateTime

public DateTime(Scanner in)
Read a date from in, leaving the file cursor immediately after the date. The date (and optional time) must be in one of the two formats produced by toString.

Parameters:
in - The open file from which to read the date.

DateTime

public DateTime(DateTime dt)
Construct a new DateTime object that represents the same date as dt.

Parameters:
dt - The DateTime object to duplicate.
Method Detail

getTodayInstance

public static DateTime getTodayInstance()
Get an instance representing today (time fields set to midnight).

Returns:
an instance representing today

getNowInstance

public static DateTime getNowInstance()
Get an instance representing the current date and time.

Returns:
an instance representing this moment

compareTo

public int compareTo(Object dt)
Compare two DateTime objects.

Specified by:
compareTo in interface Comparable
Parameters:
dt - A DateTime object to compare to this object. It must be a DateTime object in spite of the fact that the parameter has type Object.
Returns:
-1 if this object represents a date & time before dt, +1 if this object represents a date & time after dt, and 0 if they represent the same time.

setFormatLength

public void setFormatLength(int formatLength)
Set the length of the string returned by format().

Parameters:
formatLength - One of DateTime.{SHORT, MEDIUM, LONG}

setFormatInclude

public void setFormatInclude(int formatInclude)
Set the data to include in the string returned by format().

Parameters:
formatInclude - One of DateTime.{DATE_ONLY, TIME_ONLY, DATE_AND_TIME}

format

public String format()
Format the date and/or time as a string according to the current settings established by setFormatInclude and setFormatLength.
Examples:
LengthIncludeFormat
SHORTDATE_AND_TIME3/27/08 1:15 PM
SHORTDATE_ONLY3/27/08
SHORTTIME_ONLY1:15 PM
MEDIUMDATE_AND_TIMEMar 27, 2008 1:15:15 PM
MEDIUMDATE_ONLYMar 27, 2008
MEDIUMTIME_ONLY1:15:15 PM
LONGDATE_AND_TIMEMarch 27, 2008 1:15:15 PM EST
LONGDATE_ONLYMarch 27, 2008
LONGTIME_ONLY1:15:15 PM EST


toString

public String toString()
Represent this date (and time, if the time is not midnight) as a string. Example formats: 2003/3/27 and 2003/3/27@10:05:30

Overrides:
toString in class Object
Returns:
A string representing this date and time.

getYear

public int getYear()
Get the year.

Returns:
The year

getMonth

public int getMonth()
Get the month.

Returns:
The month, one of {1..12}

getDay

public int getDay()
Get the day.

Returns:
The day, one of {1..31}

getHour

public int getHour()
Get the hour.

Returns:
The hour, one of {0..23}

getMinute

public int getMinute()
Get the minute.

Returns:
The minute, one of {0..59}

getSecond

public int getSecond()
Get the second.

Returns:
The second, one of {0..59}

addYears

public void addYears(int howMany)
Add (or subtract, if negative) years from this DateTime.

Parameters:
howMany - The number of years to add or subtract.

addMonths

public void addMonths(int howMany)
Add (or subtract, if negative) months from this DateTime.

Parameters:
howMany - The number of months to add or subtract.

addDays

public void addDays(int howMany)
Add (or subtract, if negative) days from this DateTime.

Parameters:
howMany - The number of days to add or subtract.

addHours

public void addHours(int howMany)
Add (or subtract, if negative) hours from this DateTime.

Parameters:
howMany - The number of hours to add or subtract.

addMinutes

public void addMinutes(int howMany)
Add (or subtract, if negative) minutes from this DateTime.

Parameters:
howMany - The number of minutes to add or subtract.

addSeconds

public void addSeconds(int howMany)
Add (or subtract, if negative) seconds from this DateTime.

Parameters:
howMany - The number of seconds to add or subtract.

setYear

public void setYear(int year)
Set the year for this to the given year

Parameters:
year - the new year for this

setMonth

public void setMonth(int month)
Set the month for this to the given month

Parameters:
month - the new month for this

setDay

public void setDay(int day)
Set the day for this to the given day

Parameters:
day - the new day for this

setHour

public void setHour(int hour)
Set the hour for this to the given hour

Parameters:
hour - the new hour for this

setMinute

public void setMinute(int minute)
Set the minute for this to the given minute

Parameters:
minute - the new minute for this

setSecond

public void setSecond(int second)
Set the second for this to the given second

Parameters:
second - the new second for this

setTime

public void setTime(int hour,
                    int min,
                    int sec)
Set the time for this to the given hour, minute and second.

Parameters:
hour - The new hour
min - The new minute
sec - The new second

isBefore

public boolean isBefore(DateTime other)
Determine whether this Date/Time is before another.

Parameters:
other - The other DateTime to compare against
Returns:
true if this DateTime represents a date & time earlier than other.

isAfter

public boolean isAfter(DateTime other)
Determine whether this Date/Time is after another.

Parameters:
other - The other DateTime to compare against
Returns:
true if this DateTime represents a date & time later than other.

equals

public boolean equals(Object other)
Determine whether this Date/Time is equal to another.

Overrides:
equals in class Object
Parameters:
other - The other DateTime to compare against
Returns:
true if this DateTime represents a date and time equal to other

clone

public Object clone()
Clone this Date/Time object to produce another one, just like it.

Overrides:
clone in class Object
Returns:
a clone of this Date/Time object

hashCode

public int hashCode()
Returns a hash code for this object for use in HashSet and related collections.

Overrides:
hashCode in class Object

daysUntil

public int daysUntil(DateTime another)
Calculate the number of days until another date. If this date is before another, the result will be positive. If this date is after another, the result will be negative.

Parameters:
another - A DateTime object.
Returns:
The number of days between the two days.

getTime

public Date getTime()
Return an object that can be passed to an instance of DateFormat. For example,
DateTime today = new DateTime();
 DateFormat df = DateFormat.getDateInstance();
 df.format(today.getTime());
 

Returns:
an object compatible with DateFormat.
Since:
June 11, 2004