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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. private final static long min = 1000 * 60l;
  20. private final static long halfhour = min * 30l;
  21. private final static long onehour = halfhour * 2;
  22. private final static long oneday = onehour * 24l;
  23. @SuppressWarnings("deprecation")
  24. public static boolean isToday(Date date) {
  25. Date now = new Date();
  26. return now.getDate() == date.getDate() && now.getMonth() == date.getMonth() && now.getYear() == date.getYear();
  27. }
  28. @SuppressWarnings("deprecation")
  29. public static boolean isYesterday(Date date) {
  30. Date now = new Date();
  31. return now.getDate() == (date.getDate() + 1) && now.getMonth() == date.getMonth() && now.getYear() == date.getYear();
  32. }
  33. public static String duration(int days) {
  34. if (days <= 60) {
  35. return days + (days > 1 ? " days" : " day");
  36. } else if (days <= 365) {
  37. int rem = days % 30;
  38. return (days / 30) + " months, " + rem + (rem > 1 ? " days" : " day");
  39. } else {
  40. int years = days / 365;
  41. int rem = days % 365;
  42. String yearsString = years + (years > 1 ? " years" : " year");
  43. if (rem < 30) {
  44. if (rem == 0) {
  45. return yearsString;
  46. } else {
  47. return yearsString + ", " + rem + (rem > 1 ? " days" : " day");
  48. }
  49. } else {
  50. int months = rem / 30;
  51. int remDays = (rem % 30);
  52. String monthsString;
  53. if (months == 0) {
  54. monthsString = yearsString;
  55. } else {
  56. monthsString = yearsString + ", " + months + (months > 1 ? " months" : " month");
  57. }
  58. if (remDays == 0) {
  59. return monthsString;
  60. } else {
  61. return monthsString + ", " + remDays + (remDays > 1 ? " days":" day");
  62. }
  63. }
  64. }
  65. }
  66. public static int minutesAgo(Date date, long endTime, boolean roundup) {
  67. long diff = endTime - date.getTime();
  68. int mins = (int) (diff / min);
  69. if (roundup && (diff % min) >= 30)
  70. mins++;
  71. return mins;
  72. }
  73. public static int minutesAgo(Date date, boolean roundup) {
  74. return minutesAgo(date, System.currentTimeMillis(), roundup);
  75. }
  76. public static int hoursAgo(Date date, boolean roundup) {
  77. long diff = System.currentTimeMillis() - date.getTime();
  78. int hours = (int) (diff / onehour);
  79. if (roundup && (diff % onehour) >= halfhour)
  80. hours++;
  81. return hours;
  82. }
  83. public static int daysAgo(Date date, boolean roundup) {
  84. long diff = System.currentTimeMillis() - date.getTime();
  85. int days = (int) (diff / oneday);
  86. if (roundup && (diff % oneday) > 0)
  87. days++;
  88. return days;
  89. }
  90. public static String timeAgo(Date date) {
  91. return timeAgo(date, false);
  92. }
  93. public static String timeAgoCss(Date date) {
  94. return timeAgo(date, true);
  95. }
  96. private static String timeAgo(Date date, boolean css) {
  97. String ago = null;
  98. if (isToday(date) || isYesterday(date)) {
  99. int mins = minutesAgo(date, true);
  100. if (mins > 120) {
  101. if (css) {
  102. return "age1";
  103. }
  104. int hours = hoursAgo(date, true);
  105. if (hours > 23) {
  106. ago = "yesterday";
  107. } else {
  108. ago = hours + " hour" + (hours > 1 ? "s" : "") + " ago";
  109. }
  110. } else {
  111. if (css) {
  112. return "age0";
  113. }
  114. ago = mins + " min" + (mins > 1 ? "s" : "") + " ago";
  115. }
  116. } else {
  117. if (css) {
  118. return "age2";
  119. }
  120. int days = daysAgo(date, true);
  121. if (days < 365) {
  122. if (days <= 30) {
  123. ago = days + " day" + (days > 1 ? "s" : "") + " ago";
  124. } else if (days <= 90) {
  125. int weeks = days / 7;
  126. if (weeks == 12)
  127. ago = "3 months ago";
  128. else
  129. ago = weeks + " weeks ago";
  130. } else if (days > 90) {
  131. int months = days / 30;
  132. int weeks = (days % 30) / 7;
  133. if (weeks >= 2)
  134. months++;
  135. ago = months + " month" + (months > 1 ? "s" : "") + " ago";
  136. } else
  137. ago = days + " day" + (days > 1 ? "s" : "") + " ago";
  138. } else if (days == 365) {
  139. ago = "1 year ago";
  140. } else {
  141. int yr = days / 365;
  142. days = days % 365;
  143. int months = (yr * 12) + (days / 30);
  144. if (months > 23) {
  145. ago = yr + " years ago";
  146. } else {
  147. ago = months + " months ago";
  148. }
  149. }
  150. }
  151. return ago;
  152. }
  153. }