类 Date
- java.lang.Object
-
- edu.princeton.cs.algs4.Date
-
- 所有已实现的接口:
java.lang.Comparable<Date>
public class Date extends java.lang.Object implements java.lang.Comparable<Date>
TheDate
class 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.
-
-
方法概要
修饰符和类型 方法 说明 int
compareTo(Date that)
Compares two dates chronologically.int
day()
Returns the day.boolean
equals(java.lang.Object other)
Compares this date to the specified date.int
hashCode()
Returns an integer hash code for this date.boolean
isAfter(Date that)
Compares two dates chronologically.boolean
isBefore(Date that)
Compares two dates chronologically.static void
main(java.lang.String[] args)
Unit tests theDate
data type.int
month()
Return the month.Date
next()
Returns the next date in the calendar.java.lang.String
toString()
Returns a string representation of this date.int
year()
Returns the year.
-
-
-
构造器详细资料
-
Date
public Date(int month, int day, int year)
Initializes a new date from the month, day, and year.- 参数:
month
- the month (between 1 and 12)day
- the day (between 1 and 28-31, depending on the month)year
- the year- 抛出:
java.lang.IllegalArgumentException
- if this date is invalid
-
Date
public Date(java.lang.String date)
Initializes new date specified as a string in form MM/DD/YYYY.- 参数:
date
- the string representation of this date- 抛出:
java.lang.IllegalArgumentException
- if this date is invalid
-
-
方法详细资料
-
month
public int month()
Return the month.- 返回:
- the month (an integer between 1 and 12)
-
day
public int day()
Returns the day.- 返回:
- the day (an integer between 1 and 31)
-
year
public int year()
Returns the year.- 返回:
- the year
-
next
public Date next()
Returns the next date in the calendar.- 返回:
- a date that represents the next day after this day
-
isAfter
public boolean isAfter(Date that)
Compares two dates chronologically.- 参数:
that
- the other date- 返回:
true
if this date is after that date;false
otherwise
-
isBefore
public boolean isBefore(Date that)
Compares two dates chronologically.- 参数:
that
- the other date- 返回:
true
if this date is before that date;false
otherwise
-
compareTo
public int compareTo(Date that)
Compares two dates chronologically.- 指定者:
compareTo
在接口中java.lang.Comparable<Date>
- 返回:
- the value
0
if the argument date is equal to this date; a negative integer if this date is chronologically less than the argument date; and a positive ineger if this date is chronologically after the argument date
-
toString
public java.lang.String toString()
Returns a string representation of this date.- 覆盖:
toString
在类中java.lang.Object
- 返回:
- the string representation in the format MM/DD/YYYY
-
equals
public boolean equals(java.lang.Object other)
Compares this date to the specified date.- 覆盖:
equals
在类中java.lang.Object
- 参数:
other
- the other date- 返回:
true
if this date equalsother
;false
otherwise
-
hashCode
public int hashCode()
Returns an integer hash code for this date.- 覆盖:
hashCode
在类中java.lang.Object
- 返回:
- an integer hash code for this date
-
main
public static void main(java.lang.String[] args)
Unit tests theDate
data type.- 参数:
args
- the command-line arguments
-
-