Package dsa
Class Date
- java.lang.Object
-
- dsa.Date
-
- All Implemented Interfaces:
Comparable<Date>
public class Date extends Object implements Comparable<Date>
An immutable data type to represent a date (day, month, and year).
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(Date other)
Returns a chronological comparison of this date withother
.int
day()
Returns the day (an integer between 1 and 31).boolean
equals(Object other)
Returnstrue
if this date is same asother
, andfalse
otherwise.int
hashCode()
Returns a hash code for this date.boolean
isAfter(Date other)
Returnstrue
if this date is afterother
, andfalse
otherwise.boolean
isBefore(Date other)
Returnstrue
if this date is beforeother
, andfalse
otherwise.static void
main(String[] args)
Unit tests the data type.int
month()
Returns the month (an integer between 1 and 12).Date
next()
Returns the next date in the calendar.String
toString()
Returns a string representation of this date.int
year()
Returns the year.
-
-
-
Constructor Detail
-
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
public Date(String s)
Constructs a date from a strings
of the form the"MM/DD/YYYY"
.- Parameters:
s
- the string.
-
-
Method Detail
-
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
public Date next()
Returns the next date in the calendar.- Returns:
- the next date in the calendar.
-
isBefore
public boolean isBefore(Date other)
Returnstrue
if this date is beforeother
, andfalse
otherwise.- Parameters:
other
- the other date.- Returns:
true
if this date is beforeother
, andfalse
otherwise.
-
isAfter
public boolean isAfter(Date other)
Returnstrue
if this date is afterother
, andfalse
otherwise.- Parameters:
other
- the other date.- Returns:
true
if this date is afterother
, andfalse
otherwise.
-
equals
public boolean equals(Object other)
Returnstrue
if this date is same asother
, andfalse
otherwise.
-
hashCode
public int hashCode()
Returns a hash code for this date.
-
toString
public String toString()
Returns a string representation of this date.
-
compareTo
public int compareTo(Date other)
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
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-