public class EasyDate extends java.lang.Object implements java.lang.Comparable<EasyDate>
Appendix to:
Java Methods: Object-Oriented Programming and Data Structures, 3rd AP Edition
(Skylight Publishing 2015, ISBN 978-0-9824775-6-4)
EasyDate provides a simple representation of a local date with month, day, year fields. EasyDate objects are immutable.
Example: ======= EasyDate today = new EasyDate(); System.out.println("Today is " + today); EasyDate tomorrow = today.add(1); EasyDate yesterday = today.add(-1); int yr = today.getYear(); System.out.println(yr + " is a leap year? This is " + EasyDate.isLeapYear(yr)); EasyDate myBirthday = new EasyDate(bDayMonth, bDayDay, yr); if (today.equals(myBirthday)) System.out.println("Today is my birthday"); else if (yesterday.equals(myBirthday)) System.out.println("My birthday was yesterday"); else { if (myBirthday.compareTo(today) < 0) myBirthday = new EasyDate(bDayMonth, bDayDay, yr + 1); System.out.println(today.daysTo(myBirthday) + " days are left until my next birthday"); }
Modifier and Type | Field and Description |
---|---|
static long |
MILLIS_DAY
Number of milliseconds in 24 hours.
|
Constructor and Description |
---|
EasyDate()
Constructs a date for the current month, day, and year.
|
EasyDate(EasyDate other)
Constructs a date equal to a given date.
|
EasyDate(int month,
int day,
int year)
Constructs a date for given month, day, and year.
|
Modifier and Type | Method and Description |
---|---|
EasyDate |
add(int numDays)
Returns a new EasyDate that is
numDays later
than this date (or earlier, if numDays is negative). |
int |
compareTo(EasyDate other)
Compares
this date to other . |
int |
daysTo(EasyDate other)
Returns the number of days from this date to
other . |
boolean |
equals(java.lang.Object obj)
Checks whether
this date is equal to other . |
int |
getDay()
Returns the day of this EasyDate.
|
int |
getMonth()
Returns the month of this EasyDate.
|
int |
getYear()
Returns the year of this EasyDate.
|
int |
hashCode()
Returns the hash code for this date.
|
static boolean |
isLeapYear(int year)
Checks whether a given year is a leap year.
|
java.lang.String |
toString()
Returns this date as a string in the mm/dd/yyyy format.
|
public static final long MILLIS_DAY
public EasyDate()
public EasyDate(int month, int day, int year)
month
- the month (1 - 12)day
- the day (1 - 31)year
- the year (1600 - 2100)public EasyDate(EasyDate other)
other
- the date to be copied.public static boolean isLeapYear(int year)
year
- a year to be checked (1600-2100).year
is a leap year, false otherwise.public int getMonth()
public int getDay()
public int getYear()
public EasyDate add(int numDays)
numDays
later
than this date (or earlier, if numDays is negative).numDays
- the number of days to add.public int daysTo(EasyDate other)
other
.other
- an EasyDate to which the number of days is computed.public int compareTo(EasyDate other)
this
date to other
.compareTo
in interface java.lang.Comparable<EasyDate>
this
date is later than other; returns a negative integer if this
date is earlier than other
; returns 0 if this
date is the same as other
.public boolean equals(java.lang.Object obj)
this
date is equal to other
.equals
in class java.lang.Object
obj
represents the same date as
this
, false otherwise.public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object