Advertisement
kmahadev

Time2

Aug 4th, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1.  
  2. public class Time2 {
  3. private int hour;
  4. private int minute;
  5. private int second;
  6.  
  7. public Time2() {
  8. //this(0,0,0);
  9. setTime(0,0,0);
  10. }
  11.  
  12. public Time2(int h) {
  13. //this(h, 0, 0);
  14. setTime(h, 0, 0);
  15. }
  16.  
  17. public Time2(int usrhour, int usrminute){
  18. setTime(usrhour, usrminute, 0);
  19. }
  20.  
  21. public Time2(int h, int m, int s) {
  22. setTime(h, m, s);
  23. }
  24.  
  25. public Time2(Time2 t) {
  26. this.hour = t.getHour();
  27. this.minute = t.getMinute();
  28. this.second = t.getSeconds();
  29. }
  30.  
  31. private void setTime(int h, int m, int s) {
  32. setHour(h);
  33. setMinute(m);
  34. setSecond(s);
  35. }
  36.  
  37. private void setHour(int h) {
  38. hour = ( ( h >=0 && h < 24) ? h%12 : 0 );
  39. }
  40.  
  41. private void setMinute(int m) {
  42. minute = ( ( m >= 0 && m < 60) ? m : 0 );
  43. }
  44.  
  45. private void setSecond(int s) {
  46. second = ( ( s >= 0 && s < 60) ? s : 0 );
  47. }
  48.  
  49. public int getHour(){ return this.hour; }
  50.  
  51. public int getMinute() { return this.minute; }
  52.  
  53. public int getSeconds() { return this.second; }
  54.  
  55. public void addTime(int h, int m, int s) {
  56.  
  57. setTime(this.hour + h, this.minute + m, this.second + s);
  58. }
  59.  
  60. public void UpdateTime(Time2 t) {
  61. this.hour = t.hour;
  62. this.minute = t.minute;
  63. this.second = t.second;
  64. }
  65. public String toString() {
  66. return String.format("The Time is: %02d:%02d:%02d %s",
  67. this.hour, this.minute, this.second, ((hour < 12)? "AM" : "PM"));
  68. }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement