Advertisement
spigotdev

Untitled

Dec 19th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package com.AsylumDevs.AsylumMines.Utils;
  2.  
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. public class TimeAPI {
  6. private static long DAYS_IN_WEEK = 7L;
  7. private static long DAYS_IN_MONTH = 30L;
  8. private static long DAYS_IN_YEAR = 365L;
  9. private static long DAYS_IN_DECADE = 3650L;
  10. private long seconds;
  11.  
  12. public TimeAPI(String time) {
  13. reparse(time);
  14. }
  15.  
  16. public TimeAPI(int seconds) {
  17. this.seconds = seconds;
  18. }
  19.  
  20. public TimeAPI reparse(String time) {
  21. this.seconds = 0L;
  22.  
  23. TimeScanner scanner = new TimeScanner(time.replace(" ", "").replace("and", "").replace(",", "").toLowerCase());
  24. while (scanner.hasNext()) {
  25. long next = scanner.nextLong();
  26. switch (scanner.nextString()) {
  27. case "s":
  28. case "sec":
  29. case "secs":
  30. case "second":
  31. case "seconds":
  32. this.seconds += next;
  33. break;
  34. case "m":
  35. case "min":
  36. case "mins":
  37. case "minute":
  38. case "minutes":
  39. this.seconds += TimeUnit.MINUTES.toSeconds(next);
  40. break;
  41. case "h":
  42. case "hr":
  43. case "hrs":
  44. case "hour":
  45. case "hours":
  46. this.seconds += TimeUnit.HOURS.toSeconds(next);
  47. break;
  48. case "d":
  49. case "dy":
  50. case "dys":
  51. case "day":
  52. case "days":
  53. this.seconds += TimeUnit.DAYS.toSeconds(next);
  54. break;
  55. case "w":
  56. case "week":
  57. case "weeks":
  58. this.seconds += TimeUnit.DAYS.toSeconds(next * DAYS_IN_WEEK);
  59. break;
  60. case "mo":
  61. case "mon":
  62. case "mnth":
  63. case "month":
  64. case "months":
  65. this.seconds += TimeUnit.DAYS.toSeconds(next * DAYS_IN_MONTH);
  66. break;
  67. case "y":
  68. case "yr":
  69. case "yrs":
  70. case "year":
  71. case "years":
  72. this.seconds += TimeUnit.DAYS.toSeconds(next * DAYS_IN_YEAR);
  73. break;
  74. default:
  75. throw new IllegalArgumentException();
  76. }
  77. }
  78. return this;
  79. }
  80.  
  81. public double getNanoseconds() {
  82. return TimeUnit.SECONDS.toNanos(this.seconds);
  83. }
  84.  
  85. public double getMicroseconds() {
  86. return TimeUnit.SECONDS.toMicros(this.seconds);
  87. }
  88.  
  89. public double getMilliseconds() {
  90. return TimeUnit.SECONDS.toMillis(this.seconds);
  91. }
  92.  
  93. public long getSeconds() {
  94. return this.seconds;
  95. }
  96.  
  97. public long getMinutes() {
  98. return (long) (this.seconds / 60.0D);
  99. }
  100.  
  101. public double getHours() {
  102. return this.seconds / 3600.0D;
  103. }
  104.  
  105. public double getDays() {
  106. return this.seconds / 86400.0D;
  107. }
  108.  
  109. public double getWeeks() {
  110. return getDays() / DAYS_IN_WEEK;
  111. }
  112.  
  113. public double getMonths() {
  114. return getDays() / DAYS_IN_MONTH;
  115. }
  116.  
  117. public double getYears() {
  118. return getDays() / DAYS_IN_YEAR;
  119. }
  120.  
  121. public double getDecades() {
  122. return getDays() / DAYS_IN_DECADE;
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement