Guest User

Untitled

a guest
Dec 12th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. /**
  2. * Formats the amount of time in a friendly way.
  3. *
  4. * @param millis Amount of time in milliseconds.
  5. * @param nicly When the amount of time is less than 1 minute, the message will be cool.
  6. * @return Friendly description of the amount of time.
  7. */
  8. public static String friendlyAmountTime(long millis, boolean nicly)
  9. {
  10. long hours = TimeUnit.MILLISECONDS.toHours(millis);
  11. long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
  12. long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
  13. StringBuilder sb = new StringBuilder();
  14. if (nicly)
  15. {
  16. sb.append("Really fast!");
  17. }
  18. else
  19. {
  20. sb.append("< 1 second");
  21. }
  22. if (millis > 999)
  23. {
  24. sb.delete(0, sb.length());
  25. if (hours > 0)
  26. {
  27. sb.append(hours);
  28. sb.append(" hour");
  29. if (hours > 1)
  30. {
  31. sb.append("s");
  32. }
  33. }
  34. if (minutes > 0)
  35. {
  36. sb.append(" ");
  37. sb.append(minutes);
  38. sb.append(" minute");
  39. if (minutes > 1)
  40. {
  41. sb.append("s");
  42. }
  43. }
  44. if (seconds > 0)
  45. {
  46. sb.append(" ");
  47. sb.append(seconds);
  48. sb.append(" second");
  49. if (seconds > 1)
  50. {
  51. sb.append("s");
  52. }
  53. }
  54. }
  55.  
  56. return sb.toString().trim();
  57. }
Add Comment
Please, Sign In to add comment