public class SimpleTime extends Object implements Comparable<SimpleTime>
SimpleTime
does not
automatically change with the passing of time; a client is able to manually
set the time using the provided mutator methods.Constructor and Description |
---|
SimpleTime()
Create a
SimpleTime representing midnight (12:00 AM). |
SimpleTime(int hour,
int minute,
boolean am)
Create a
SimpleTime representing the time with the given hour
and minute. |
SimpleTime(SimpleTime other)
Create a
SimpleTime object that represents the same time
as another SimpleTime object. |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(SimpleTime other)
Compares this
SimpleTime object to another
SimpleTime object. |
boolean |
equals(Object obj)
Compare this
SimpleTime to an object for equality. |
int |
getHour()
Get the hour of the day (1-12) represented by this object.
|
int |
getMinute()
Get the minute of the hour (0-59) represented by this object.
|
int |
hashCode()
Generate a hash code for this object.
|
boolean |
isAM()
Returns
true if the time represented by this object is in the
AM, and false otherwise. |
void |
setAM(boolean am)
Set the time to AM or PM.
|
void |
setHour(int hour)
Set the hour of the day (1-12) represented by this object.
|
void |
setMinute(int minute)
Set the minute of the hour (0-59) represented by this object.
|
String |
toString()
Returns a string representation of the time of day.
|
public SimpleTime()
SimpleTime
representing midnight (12:00 AM).IllegalArgumentException
- if hour
is not in the range 1-12IllegalArgumentException
- if minute
is not in the range 0-59public SimpleTime(SimpleTime other)
SimpleTime
object that represents the same time
as another SimpleTime
object.other
- the SimpleTime
object to copypublic SimpleTime(int hour, int minute, boolean am)
SimpleTime
representing the time with the given hour
and minute.
If isAM
is true
then the object represents a time
that is between midnight and just before noon (12:00 AM-11:59 AM),
otherwise the object represents a time that is between noon and just before
midnight (12:00 PM-11:59 PM).
IllegalArgumentException
- if hour
is not in the range 1-12IllegalArgumentException
- if minute
is not in the range 0-59hour
- the hour of the dayminute
- the minute of the houram
- use true to indicate a time in the AM; use
false
to indicate a time in the PM.public int getHour()
public void setHour(int hour)
IllegalArgumentException
- if hour
is not in the range 1-12hour
- the hour to setpublic int getMinute()
public void setMinute(int minute)
IllegalArgumentException
- if minute
is not in the range 0-59minute
- the minute to setpublic boolean isAM()
true
if the time represented by this object is in the
AM, and false
otherwise.true
if the time represented by this object is in the
AM, and false
otherwise.public void setAM(boolean am)
am
- use true
to indicate a time between midnight and just
before noon (12:00 AM-11:59 AM), and false
to
represent a time that is between noon and just before midnight
(12:00 PM-11:59 PM).public int hashCode()
public boolean equals(Object obj)
SimpleTime
to an object for equality.
The result is true
if and only if obj
is a
SimpleTime
object, and both objects represent the same time of
day (i.e., identical hour, minute, and AM/PM).
public String toString()
XX:YY AM
or XX:YY PM
where XX
is the hour of the day and YY
is the minute of the day. Both
the hour and minute fields are always two characters wide.public int compareTo(SimpleTime other)
SimpleTime
object to another
SimpleTime
object.
The result is the difference in minutes between this
object
and the other
object. For example, if the time represented by
this
object is 11:00 AM and the time represented by the
other
object is 1:05 PM then
this.compareTo(other)
returns -125
.
compareTo
in interface Comparable<SimpleTime>
this
object and the
other
object.