public class Time1
1: // Fig. 8.1: Time1.java
2: // Time1 class declaration maintains the time in 24-hour format.
3:
4: public class Time1
5: {
6: private int hour; // 0 - 23
7: private int minute; // 0 - 59
8: private int second; // 0 - 59
9:
10: // set a new time value using universal time; ensure that
11: // the data remains consistent by setting invalid values to zero
12: public void setTime( int h, int m, int s )
13: {
14: hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); // validate hour
15: minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); // validate minute
16: second = ( ( s >= 0 && s < 60 ) ? s : 0 ); // validate second
17: } // end method setTime
18:
19: // convert to String in universal-time format (HH:MM:SS)
20: public String toUniversalString()
21: {
22: return String.format( "%02d:%02d:%02d", hour, minute, second );
23: } // end method toUniversalString
24:
25: // convert to String in standard-time format (H:MM:SS AM or PM)
26: public String toString()
27: {
28: return String.format( "%d:%02d:%02d %s",
29: ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
30: minute, second, ( hour < 12 ? "AM" : "PM" ) );
31: } // end method toString
32: } // end class Time1
33:
34: /**************************************************************************
35: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
36: * Pearson Education, Inc. All Rights Reserved. *
37: * *
38: * DISCLAIMER: The authors and publisher of this book have used their *
39: * best efforts in preparing the book. These efforts include the *
40: * development, research, and testing of the theories and programs *
41: * to determine their effectiveness. The authors and publisher make *
42: * no warranty of any kind, expressed or implied, with regard to these *
43: * programs or to the documentation contained in these books. The authors *
44: * and publisher shall not be liable in any event for incidental or *
45: * consequential damages in connection with, or arising out of, the *
46: * furnishing, performance, or use of these programs. *
47: *************************************************************************/