Package dsa
Class Date
java.lang.Object
dsa.Date
- All Implemented Interfaces:
Comparable<Date>
An immutable data type to represent a date (day, month, and year).
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Returns a chronological comparison of this date withother
.int
day()
Returns the day (an integer between 1 and 31).boolean
Returnstrue
if this date is same asother
, andfalse
otherwise.int
hashCode()
Returns a hash code for this date.boolean
Returnstrue
if this date is afterother
, andfalse
otherwise.boolean
Returnstrue
if this date is beforeother
, andfalse
otherwise.static void
Unit tests the data type.int
month()
Returns the month (an integer between 1 and 12).next()
Returns the next date in the calendar.toString()
Returns a string representation of this date.int
year()
Returns the year.
-
Constructor Details
-
Date
public Date(int month, int day, int year) Constructs a date frommonth
,day
, andyear
.- Parameters:
month
- the month (between 1 and 12).day
- the day (between 1 and 28-31, depending on the month).year
- the year.
-
Date
Constructs a date from a strings
of the form the"MM/DD/YYYY"
.- Parameters:
s
- the string.
-
-
Method Details
-
month
public int month()Returns the month (an integer between 1 and 12).- Returns:
- the month (an integer between 1 and 12).
-
day
public int day()Returns the day (an integer between 1 and 31).- Returns:
- the day (an integer between 1 and 31).
-
year
public int year()Returns the year.- Returns:
- the year.
-
next
Returns the next date in the calendar.- Returns:
- the next date in the calendar.
-
isBefore
Returnstrue
if this date is beforeother
, andfalse
otherwise.- Parameters:
other
- the other date.- Returns:
true
if this date is beforeother
, andfalse
otherwise.
-
isAfter
Returnstrue
if this date is afterother
, andfalse
otherwise.- Parameters:
other
- the other date.- Returns:
true
if this date is afterother
, andfalse
otherwise.
-
equals
Returnstrue
if this date is same asother
, andfalse
otherwise. -
hashCode
public int hashCode()Returns a hash code for this date. -
toString
Returns a string representation of this date. -
compareTo
Returns a chronological comparison of this date withother
.- Specified by:
compareTo
in interfaceComparable<Date>
- Parameters:
other
- the other date.- Returns:
- a chronological comparison of this date with
other
.
-
main
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-