You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimeUtils.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.util.Date;
  18. public class TimeUtils {
  19. public static final long MIN = 1000 * 60L;
  20. public static final long HALFHOUR = MIN * 30L;
  21. public static final long ONEHOUR = HALFHOUR * 2;
  22. public static final long ONEDAY = ONEHOUR * 24L;
  23. public static final long ONEYEAR = ONEDAY * 365L;
  24. @SuppressWarnings("deprecation")
  25. public static boolean isToday(Date date) {
  26. Date now = new Date();
  27. return now.getDate() == date.getDate() && now.getMonth() == date.getMonth()
  28. && now.getYear() == date.getYear();
  29. }
  30. @SuppressWarnings("deprecation")
  31. public static boolean isYesterday(Date date) {
  32. Date now = new Date();
  33. return now.getDate() == (date.getDate() + 1) && now.getMonth() == date.getMonth()
  34. && now.getYear() == date.getYear();
  35. }
  36. public static String duration(int days) {
  37. if (days <= 60) {
  38. return days + (days > 1 ? " days" : " day");
  39. } else if (days <= 365) {
  40. int rem = days % 30;
  41. return (days / 30) + " months, " + rem + (rem > 1 ? " days" : " day");
  42. } else {
  43. int years = days / 365;
  44. int rem = days % 365;
  45. String yearsString = years + (years > 1 ? " years" : " year");
  46. if (rem < 30) {
  47. if (rem == 0) {
  48. return yearsString;
  49. } else {
  50. return yearsString + ", " + rem + (rem > 1 ? " days" : " day");
  51. }
  52. } else {
  53. int months = rem / 30;
  54. int remDays = rem % 30;
  55. String monthsString;
  56. if (months == 0) {
  57. monthsString = yearsString;
  58. } else {
  59. monthsString = yearsString + ", " + months
  60. + (months > 1 ? " months" : " month");
  61. }
  62. if (remDays == 0) {
  63. return monthsString;
  64. } else {
  65. return monthsString + ", " + remDays + (remDays > 1 ? " days" : " day");
  66. }
  67. }
  68. }
  69. }
  70. public static int minutesAgo(Date date, long endTime, boolean roundup) {
  71. long diff = endTime - date.getTime();
  72. int mins = (int) (diff / MIN);
  73. if (roundup && (diff % MIN) >= 30) {
  74. mins++;
  75. }
  76. return mins;
  77. }
  78. public static int minutesAgo(Date date, boolean roundup) {
  79. return minutesAgo(date, System.currentTimeMillis(), roundup);
  80. }
  81. public static int hoursAgo(Date date, boolean roundup) {
  82. long diff = System.currentTimeMillis() - date.getTime();
  83. int hours = (int) (diff / ONEHOUR);
  84. if (roundup && (diff % ONEHOUR) >= HALFHOUR) {
  85. hours++;
  86. }
  87. return hours;
  88. }
  89. public static int daysAgo(Date date, boolean roundup) {
  90. long diff = System.currentTimeMillis() - date.getTime();
  91. int days = (int) (diff / ONEDAY);
  92. if (roundup && (diff % ONEDAY) > 0) {
  93. days++;
  94. }
  95. return days;
  96. }
  97. public static String timeAgo(Date date) {
  98. return timeAgo(date, false);
  99. }
  100. public static String timeAgoCss(Date date) {
  101. return timeAgo(date, true);
  102. }
  103. private static String timeAgo(Date date, boolean css) {
  104. String ago = null;
  105. if (isToday(date) || isYesterday(date)) {
  106. int mins = minutesAgo(date, true);
  107. if (mins > 120) {
  108. if (css) {
  109. return "age1";
  110. }
  111. int hours = hoursAgo(date, true);
  112. if (hours > 23) {
  113. ago = "yesterday";
  114. } else {
  115. ago = hours + " hour" + (hours > 1 ? "s" : "") + " ago";
  116. }
  117. } else {
  118. if (css) {
  119. return "age0";
  120. }
  121. ago = mins + " min" + (mins > 1 ? "s" : "") + " ago";
  122. }
  123. } else {
  124. if (css) {
  125. return "age2";
  126. }
  127. int days = daysAgo(date, true);
  128. if (days < 365) {
  129. if (days <= 30) {
  130. ago = days + " day" + (days > 1 ? "s" : "") + " ago";
  131. } else if (days <= 90) {
  132. int weeks = days / 7;
  133. if (weeks == 12) {
  134. ago = "3 months ago";
  135. } else {
  136. ago = weeks + " weeks ago";
  137. }
  138. } else if (days > 90) {
  139. int months = days / 30;
  140. int weeks = (days % 30) / 7;
  141. if (weeks >= 2) {
  142. months++;
  143. }
  144. ago = months + " month" + (months > 1 ? "s" : "") + " ago";
  145. } else {
  146. ago = days + " day" + (days > 1 ? "s" : "") + " ago";
  147. }
  148. } else if (days == 365) {
  149. ago = "1 year ago";
  150. } else {
  151. int yr = days / 365;
  152. days = days % 365;
  153. int months = (yr * 12) + (days / 30);
  154. if (months > 23) {
  155. ago = yr + " years ago";
  156. } else {
  157. ago = months + " months ago";
  158. }
  159. }
  160. }
  161. return ago;
  162. }
  163. }