ilham_syamsuddin

Untitled

Sep 19th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 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. public Time2(int h,int m)
  17. {
  18. this(h,m,0);
  19. }
  20. public Time2(int h,int m,int s)
  21. {
  22. setTime( h, m, s );
  23. }
  24. public Time2( Time2 time )
  25. {
  26. this(time.getHour(),time.getMinute(),time.getSecond());
  27. }
  28. //set
  29. public void setTime(int h,int m,int s)
  30. {
  31. setHour(h);
  32. setMinute(m);
  33. setSecond(s);
  34. }
  35. public void setHour(int h)
  36. {
  37. if(h>=0&&h<24)
  38. hour=h;
  39. else throw new IllegalArgumentException( "hour must be 0-23" );
  40. }
  41. public void setMinute(int m)
  42. {
  43. if(m>=0&&m<60)
  44. minute=m;
  45. else throw new IllegalArgumentException( "minute must be 0-59" );
  46. }
  47. public void setSecond(int s)
  48. {
  49. if(s>=0&&s<60)
  50. second = ( ( s >= 0 && s <60 )?s:0 );
  51. else throw new IllegalArgumentException( "second must be 0-59" );
  52. }
  53. //get
  54. public int getHour()
  55. {
  56. return hour;
  57. }
  58. public int getMinute()
  59. {
  60. return minute;
  61. }
  62. public int getSecond()
  63. {
  64. return second;
  65. }
  66. //string
  67. public String toUniversalString()
  68. {
  69. return String.format(
  70. "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
  71. }
  72. public String toString()
  73. {
  74. return String.format( "%d:%02d:%02d %s",
  75. ( (getHour() == 0 || getHour() == 12)?12 : getHour() % 12 ),
  76. getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
  77. }
  78. }
Add Comment
Please, Sign In to add comment