Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // flg 8.1 time1.java
- // time1 class declaration maintains the time in 24 hour format
- public class Time1
- {
- private int hour;
- private int minute;
- private int second;
- //set a new time value using universal time;
- //throw an exception if the hour, minutes, second is invalid
- public void setTime(int h, int m, int s)
- {
- // validate hour,minute, second
- if((h >= 0 && h< 24 ) && (m >= 0 && h< 60 ) && (s >= 0 && h< 60 ))
- {
- hour = h;
- minute = m;
- second = s;
- }
- else
- throw new IllegalArgumentException(
- "hour, minute, second and/or was out of range");
- }// end of methods
- // convert to string in universal-time format (HH:MM:SS)
- public String toUniversalString()
- {
- return String.format("%02d:%02d:%02d",hour,minute,second);
- }
- // convert to string in Standard-time format (HH:MM:SS AM or PM)
- public String toString()
- {
- return String.format("%d:%02d:%02d %s",
- ((hour == 0 || hour == 12) ? 12 : hour % 12),
- minute, second,(hour <12 ? "AM" : "PM"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement