Class TimeOfDay
- java.lang.Object
-
- TimeOfDay
-
public class TimeOfDay extends Object
A class that represents the time of day in hours and minutes such as might be used on a digital alarm clock. Times before noon (AM) are different from times from noon onwards (PM).Times start at 12:00 AM (midnight) and go up to 11:59 PM.
-
-
Constructor Summary
Constructors Constructor Description TimeOfDay()
Initializes this time to 12:00 AM (midnight).TimeOfDay(int hour, int minute, boolean isAM)
Initializes this time to the specified hours and minutes.TimeOfDay(TimeOfDay other)
Initializes this time by copying the hour, minute, and AM/PM indicator form anotherTimeOfDay
instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TimeOfDay
advance(int minutes)
Advances the time forward by the specified number of minutes, or winds back the time by the specified number of minutes ifminutes
is negative.int
getHour()
Returns the hour of this time.int
getMinute()
Returns the minute of this time.boolean
isAM()
Returns true if the time is before noon, and false otherwise.TimeOfDay
setHour(int hour)
Sets the hour of this time to the specified hour.TimeOfDay
setMinute(int minute)
Sets the minute of this time to the specified minute.String
toString()
Returns a string representation of this time.
-
-
-
Constructor Detail
-
TimeOfDay
public TimeOfDay()
Initializes this time to 12:00 AM (midnight).
-
TimeOfDay
public TimeOfDay(int hour, int minute, boolean isAM)
Initializes this time to the specified hours and minutes. IfisAM
is true then the time is before noon, otherwise the time is from noon onwards.- Parameters:
hour
- the hour of this timeminute
- the minute of this timeisAM
- if true then the time is before noon, otherwise the time is from noon onwards- Throws:
IllegalArgumentException
- if hour is not between 1 and 12, inclusiveIllegalArgumentException
- if minute is not between 0 and 59, inclusive
-
TimeOfDay
public TimeOfDay(TimeOfDay other)
Initializes this time by copying the hour, minute, and AM/PM indicator form anotherTimeOfDay
instance.- Parameters:
other
- the time to copy
-
-
Method Detail
-
getHour
public int getHour()
Returns the hour of this time. The returned value is between 1 and 12, inclusive.- Returns:
- the hour of this time
-
getMinute
public int getMinute()
Returns the minute of this time. The returned value is between 0 and 59, inclusive.- Returns:
- the minute of this time
-
setHour
public TimeOfDay setHour(int hour)
Sets the hour of this time to the specified hour.- Parameters:
hour
- the new hour of this time- Returns:
- a reference to this object
- Throws:
IllegalArgumentException
- if hour is not between 1 and 12, inclusive
-
setMinute
public TimeOfDay setMinute(int minute)
Sets the minute of this time to the specified minute.- Parameters:
minute
- the new minute of this time- Returns:
- a reference to this object
- Throws:
IllegalArgumentException
- if minute is not between 0 and 59, inclusive
-
isAM
public boolean isAM()
Returns true if the time is before noon, and false otherwise.- Returns:
- true if the time is before noon, and false otherwise
-
advance
public TimeOfDay advance(int minutes)
Advances the time forward by the specified number of minutes, or winds back the time by the specified number of minutes ifminutes
is negative. For example, if the current time is 10:15 AM then advancing the time by +3 minutes changes the time to 10:18 AM. If the current time is 10:15 AM then advancing the time by -3 minutes changes the time to 10:12 AM.The time can be advanced past the end of the day in which case the time wraps around towards the start of the day. For example, if the current time is 11:59 PM then advancing the time by +2 minutes changes the time to 12:01 AM.
Similarly, the time can be wound back past the start of the day, in which case the time wraps around towards the end of the day. For example, if the current time is 12:01 AM then advancing the time by -2 minutes changes the time to 11:59 PM.
- Parameters:
minutes
- the number of minutes to advance the time- Returns:
- a reference to this object
-
toString
public String toString()
Returns a string representation of this time. The string representation is the hour followed by a colon followed by the minutes followed by a space followed by the string"AM"
or"PM"
. For example the string representation of the time five minutes after three in the afternoon is the string equal to"3:05 PM"
.
-
-