Guest User

Untitled

a guest
Aug 25th, 2019
305
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static String formatar(long time) {
  2. if (time == 0) {
  3. return "never";
  4. }
  5. long day = TimeUnit.MILLISECONDS.toDays(time);
  6. long hours = TimeUnit.MILLISECONDS.toHours(time) - (day * 24);
  7. long minutes = TimeUnit.MILLISECONDS.toMinutes(time) - (TimeUnit.MILLISECONDS.toHours(time) * 60);
  8. long seconds = TimeUnit.MILLISECONDS.toSeconds(time) - (TimeUnit.MILLISECONDS.toMinutes(time) * 60);
  9. StringBuilder sb = new StringBuilder();
  10. if (day > 0) {
  11. sb.append(day).append(" ").append(day == 1 ? "dia" : "dias").append(" ");
  12. }
  13. if (hours > 0) {
  14. sb.append(hours).append(" ").append(hours == 1 ? "hora" : "horas").append(" ");
  15. }
  16. if (minutes > 0) {
  17. sb.append(minutes).append(" ").append(minutes == 1 ? "minuto" : "minutos").append(" ");
  18. }
  19. if (seconds > 0) {
  20. sb.append(seconds).append(" ").append(seconds == 1 ? "segundo" : "segundos");
  21. }
  22. String diff = sb.toString();
  23. return diff.isEmpty() ? "agora" : diff;
  24. }
RAW Paste Data