Advertisement
ilminottaken

Untitled

Sep 21st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. public class Time2
  2. {
  3. private int hour;
  4. private int minute;
  5. private int second;
  6.  
  7. public Time2()
  8. {
  9. this( 0, 0, 0 );
  10. }
  11.  
  12. public Time2( int h )
  13. {
  14. this( h, 0, 0 );
  15. }
  16.  
  17. public Time2( int h, int m )
  18. {
  19. this( h, m, 0 );
  20. }
  21.  
  22. public Time2( int h, int m, int s )
  23. {
  24. setTime( h, m, s );
  25. }
  26.  
  27. public Time2( Time2 time )
  28. {
  29. this( time.getHour(), time.getMinute(), time.getSecond() );
  30. }
  31.  
  32. public void setTime( int h, int m, int s )
  33. {
  34. setHour( h );
  35. setMinute( m );
  36. setSecond( s );
  37. }
  38. public void setHour( int h )
  39. {
  40. if ( h >= 0 && h < 24 )
  41. hour = h;
  42. else
  43. throw new IllegalArgumentException( "hour must be 0-23" );
  44. }
  45. public void setMinute( int m )
  46. {
  47. if ( m >= 0 && m < 60 )
  48. minute = m;
  49. else
  50. throw new IllegalArgumentException( "minute must be 0-59" );
  51. }
  52. public void setSecond( int s )
  53. {
  54. if ( s >= 0 && s < 60 )
  55. second = ( ( s >= 0 && s < 60 ) ? s : 0 );
  56. else
  57. throw new IllegalArgumentException( "second must be 0-59" );
  58. }
  59. public int getHour()
  60. {
  61. return hour;
  62. }
  63. public int getMinute()
  64. {
  65. return minute;
  66. }
  67.  
  68. public int getSecond()
  69. {
  70. return second;
  71. }
  72. public String toUniversalString()
  73. {
  74. return String.format(
  75. "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
  76. }
  77. public String toString()
  78. {
  79. return String.format( "%d:%02d:%02d %s",
  80. ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
  81. getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement