1+ package src ;
2+
3+
4+ public class MyTime {
5+
6+ private int hour = 0 ;
7+ private int minute = 0 ;
8+ private int second = 0 ;
9+
10+ public MyTime (int hour , int minute , int second ) {
11+ setTime (hour , minute , second );
12+ }
13+
14+ /**
15+ * [setTime description]
16+ * @param hour [description]
17+ * @param minute [description]
18+ * @param second [description]
19+ */
20+ public void setTime (int hour , int minute , int second )
21+ {
22+ setHour (hour );
23+ setMinute (minute );
24+ setSecond (second );
25+ }
26+
27+ /**
28+ * [getHour description]
29+ * @return [description]
30+ */
31+ public int getHour () {
32+ return hour ;
33+ }
34+
35+ /**
36+ * [getMinute description]
37+ * @return [description]
38+ */
39+ public int getMinute () {
40+ return minute ;
41+ }
42+
43+ /**
44+ * [getSecond description]
45+ * @return [description]
46+ */
47+ public int getSecond () {
48+ return second ;
49+ }
50+
51+ /**
52+ * [setHour description]
53+ * @param hour [description]
54+ */
55+ public void setHour (int hour ) {
56+ if (hour < 0 || hour > 23 ) {
57+ throw new IllegalArgumentException ("Not valid hour's value" );
58+ }
59+ this .hour = hour ;
60+ }
61+
62+ /**
63+ * [setMinute description]
64+ * @param minute [description]
65+ */
66+ public void setMinute (int minute ) {
67+ if (minute < 0 || minute > 59 ) {
68+ throw new IllegalArgumentException ("Not valid minute's value" );
69+ }
70+ this .minute = minute ;
71+ }
72+
73+ /**
74+ * [setSecond description]
75+ * @param second [description]
76+ */
77+ public void setSecond (int second ) {
78+ if (second < 0 || second > 59 ) {
79+ throw new IllegalArgumentException ("Not valid second's value" );
80+ }
81+ this .second = second ;
82+ }
83+
84+ /**
85+ * [nextSecond description]
86+ * @return [description]
87+ */
88+ public MyTime nextSecond ()
89+ {
90+ try {
91+ setTime (hour , minute , ++second );
92+ return this ;
93+ }
94+ catch (IllegalArgumentException e ) {}
95+
96+ second = 0 ;
97+ return nextMinute ();
98+ }
99+
100+ /**
101+ * [nextMinute description]
102+ * @return [description]
103+ */
104+ public MyTime nextMinute ()
105+ {
106+ try {
107+ setTime (hour , ++minute , second );
108+ return this ;
109+ }
110+ catch (IllegalArgumentException e ) {}
111+
112+ minute = 0 ;
113+ return nextHour ();
114+ }
115+
116+ /**
117+ * [nextHour description]
118+ * @return [description]
119+ */
120+ public MyTime nextHour ()
121+ {
122+ try {
123+ setTime (++hour , minute , second );
124+ return this ;
125+ }
126+ catch (IllegalArgumentException e ) {}
127+
128+ hour = 0 ;
129+ return this ;
130+ }
131+
132+ /**
133+ * [previousSecond description]
134+ * @return [description]
135+ */
136+ public MyTime previousSecond ()
137+ {
138+ try {
139+ setTime (hour , minute , --second );
140+ return this ;
141+ }
142+ catch (IllegalArgumentException e ) {}
143+
144+ second = 59 ;
145+ return previousMinute ();
146+ }
147+
148+ /**
149+ * [previousMinute description]
150+ * @return [description]
151+ */
152+ public MyTime previousMinute () {
153+ try {
154+ setTime (hour , --minute , second );
155+ return this ;
156+ }
157+ catch (IllegalArgumentException e ) {}
158+
159+ minute = 59 ;
160+ return previousHour ();
161+ }
162+
163+ /**
164+ * [previousHour description]
165+ * @return [description]
166+ */
167+ public MyTime previousHour () {
168+ try {
169+ setTime (--hour , minute , second );
170+ return this ;
171+ }
172+ catch (IllegalArgumentException e ) {}
173+
174+ hour = 23 ;
175+ return this ;
176+ }
177+
178+ /**
179+ * [toString description]
180+ * @return [description]
181+ */
182+ public String toString () {
183+ return String .format ("%1$02d:%2$02d:%3$02d" , hour , minute , second );
184+ }
185+ }
0 commit comments