Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Java Time Practice
- Nikodemus Siahaan
- 05111840000151
- PBO A
- */
- public class ClockTime
- {
- private int hour; // 0-23
- private int minute; // 0-59
- private int second; // 0-59
- public ClockTime(int hour, int minute, int second)
- {
- if((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60))
- { this.hour = hour;
- this.minute = minute;
- this.second = second;
- }
- else {
- this.hour=0;
- this.minute=0;
- this.second=0;
- }
- }
- public String to24hourformat()
- {
- return String.format ("%02d:%02d:%02d", hour, minute, second);
- }
- public String to12hourformat()
- {
- String AMorPM;
- if(hour < 12) {
- AMorPM = "AM";
- }
- else{ AMorPM = "PM";
- }
- if(hour == 0 || hour == 12)
- { hour = 12;
- }
- else
- { hour = hour % 12;
- }
- return String.format ("%d:%02d:%02d %s", hour, minute, second, AMorPM);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement