Package edu.princeton.cs.algs4
Class Date
- Object
-
- edu.princeton.cs.algs4.Date
-
- All Implemented Interfaces:
Comparable<Date>
public class Date extends Object implements Comparable<Date>
TheDateclass is an immutable data type to encapsulate a date (day, month, and year).For additional documentation, see Section 1.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(Date that)Compares two dates chronologically.intday()Returns the day.booleanequals(Object other)Compares this date to the specified date.inthashCode()Returns an integer hash code for this date.booleanisAfter(Date that)Compares two dates chronologically.booleanisBefore(Date that)Compares two dates chronologically.static voidmain(String[] args)Unit tests theDatedata type.intmonth()Return the month.Datenext()Returns the next date in the calendar.StringtoString()Returns a string representation of this date.intyear()Returns the year.
-
-
-
Constructor Detail
-
Date
public Date(int month, int day, int year)Initializes a new date from the month, day, and year.- Parameters:
month- the month (between 1 and 12)day- the day (between 1 and 28-31, depending on the month)year- the year- Throws:
IllegalArgumentException- if this date is invalid
-
Date
public Date(String date)
Initializes new date specified as a string in form MM/DD/YYYY.- Parameters:
date- the string representation of this date- Throws:
IllegalArgumentException- if this date is invalid
-
-
Method Detail
-
month
public int month()
Return the month.- Returns:
- the month (an integer between 1 and 12)
-
day
public int day()
Returns the day.- 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:
- a date that represents the next day after this day
-
isAfter
public boolean isAfter(Date that)
Compares two dates chronologically.- Parameters:
that- the other date- Returns:
trueif this date is after that date;falseotherwise
-
isBefore
public boolean isBefore(Date that)
Compares two dates chronologically.- Parameters:
that- the other date- Returns:
trueif this date is before that date;falseotherwise
-
compareTo
public int compareTo(Date that)
Compares two dates chronologically.- Specified by:
compareToin interfaceComparable<Date>- Returns:
- the value
0if the argument date is equal to this date; a negative integer if this date is chronologically less than the argument date; and a positive integer if this date is chronologically after the argument date
-
toString
public String toString()
Returns a string representation of this date.
-
equals
public boolean equals(Object other)
Compares this date to the specified date.
-
hashCode
public int hashCode()
Returns an integer hash code for this date.
-
main
public static void main(String[] args)
Unit tests theDatedata type.- Parameters:
args- the command-line arguments
-
-