Package dsa

Class 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).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Date(int month, int day, int year)
    Constructs a date from month, day, and year.
    Constructs a date from a string s of the form the "MM/DD/YYYY".
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    compareTo(Date other)
    Returns a chronological comparison of this date with other.
    int
    day()
    Returns the day (an integer between 1 and 31).
    boolean
    equals(Object other)
    Returns true if this date is same as other, and false otherwise.
    int
    Returns a hash code for this date.
    boolean
    isAfter(Date other)
    Returns true if this date is after other, and false otherwise.
    boolean
    isBefore(Date other)
    Returns true if this date is before other, and false otherwise.
    static void
    main(String[] args)
    Unit tests the data type.
    int
    Returns the month (an integer between 1 and 12).
    Returns the next date in the calendar.
    Returns a string representation of this date.
    int
    Returns the year.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Date

      public Date(int month, int day, int year)
      Constructs a date from 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.
    • Date

      public Date(String s)
      Constructs a date from a string s 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

      public Date next()
      Returns the next date in the calendar.
      Returns:
      the next date in the calendar.
    • isBefore

      public boolean isBefore(Date other)
      Returns true if this date is before other, and false otherwise.
      Parameters:
      other - the other date.
      Returns:
      true if this date is before other, and false otherwise.
    • isAfter

      public boolean isAfter(Date other)
      Returns true if this date is after other, and false otherwise.
      Parameters:
      other - the other date.
      Returns:
      true if this date is after other, and false otherwise.
    • equals

      public boolean equals(Object other)
      Returns true if this date is same as other, and false otherwise.
      Overrides:
      equals in class Object
      Parameters:
      other - the other date.
      Returns:
      true if this date is same as other, and false otherwise.
    • hashCode

      public int hashCode()
      Returns a hash code for this date.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code for this date
    • toString

      public String toString()
      Returns a string representation of this date.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this date.
    • compareTo

      public int compareTo(Date other)
      Returns a chronological comparison of this date with other.
      Specified by:
      compareTo in interface Comparable<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.