Advertisement
Guest User

Willow Chop'N'Sell - Timer.java

a guest
Nov 21st, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package scripts;
  2.  
  3. /**
  4. * A Timer
  5. */
  6. public class Timer {
  7.  
  8. private long end;
  9. private final long start;
  10. private final long period;
  11.  
  12. /**
  13. * Instantiates a new Timer with a given time period in milliseconds.
  14. *
  15. * @param period
  16. * Time period in milliseconds.
  17. */
  18. public Timer(long period) {
  19. this.period = period;
  20. this.start = System.currentTimeMillis();
  21. this.end = start + period;
  22. }
  23.  
  24. /**
  25. * Returns the number of milliseconds elapsed since the start time.
  26. *
  27. * @return The elapsed time in milliseconds.
  28. */
  29. public long getElapsed() {
  30. return (System.currentTimeMillis() - start);
  31. }
  32.  
  33. /**
  34. * Returns the number of milliseconds remaining until the timer is up.
  35. *
  36. * @return The remaining time in milliseconds.
  37. */
  38. public long getRemaining() {
  39. if (isRunning()) {
  40. return (end - System.currentTimeMillis());
  41. }
  42. return 0;
  43. }
  44.  
  45. /**
  46. * Returns <tt>true</tt> if this timer's time period has not yet elapsed.
  47. *
  48. * @return <tt>true</tt> if the time period has not yet passed.
  49. */
  50. public boolean isRunning() {
  51. return (System.currentTimeMillis() < end);
  52. }
  53.  
  54. /**
  55. * Restarts this timer using its period.
  56. */
  57. public void reset() {
  58. this.end = System.currentTimeMillis() + period;
  59. }
  60.  
  61. /**
  62. * Sets the end time of this timer to a given number of milliseconds from
  63. * the time it is called. This does not edit the period of the timer (so
  64. * will not affect operation after reset).
  65. *
  66. * @param ms
  67. * The number of milliseconds before the timer should stop
  68. * running.
  69. * @return The new end time.
  70. */
  71. public long setEndIn(long ms) {
  72. this.end = System.currentTimeMillis() + ms;
  73. return this.end;
  74. }
  75.  
  76. /**
  77. * Returns a formatted String of the time elapsed.
  78. *
  79. * @return The elapsed time formatted hh:mm:ss.
  80. */
  81. public String toElapsedString() {
  82. return format(getElapsed());
  83. }
  84.  
  85. /**
  86. * Returns a formatted String of the time remaining.
  87. *
  88. * @return The remaining time formatted hh:mm:ss.
  89. */
  90. public String toRemainingString() {
  91. return format(getRemaining());
  92. }
  93.  
  94. /**
  95. * Converts milliseconds to a String in the format hh:mm:ss.
  96. *
  97. * @param time
  98. * The number of milliseconds.
  99. * @return The formatted String.
  100. */
  101. public static String format(long time) {
  102. StringBuilder t = new StringBuilder();
  103. long total_secs = time / 1000;
  104. long total_mins = total_secs / 60;
  105. long total_hrs = total_mins / 60;
  106. int secs = (int) total_secs % 60;
  107. int mins = (int) total_mins % 60;
  108. int hrs = (int) total_hrs % 60;
  109. if (hrs < 10) {
  110. t.append("0");
  111. }
  112. t.append(hrs);
  113. t.append(":");
  114. if (mins < 10) {
  115. t.append("0");
  116. }
  117. t.append(mins);
  118. t.append(":");
  119. if (secs < 10) {
  120. t.append("0");
  121. }
  122. t.append(secs);
  123. return t.toString();
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement