code: public static String formatDuration(final long startTime, final long endTime) {
      if (startTime == -1) {
        return "-";
      }
  
      final long durationMS;
      if (endTime == -1) {
        durationMS = System.currentTimeMillis() - startTime;
      } else {
        durationMS = endTime - startTime;
      }
  
      long seconds = durationMS / 1000;
      if (seconds < 60) {
        return seconds + " sec";
      }
  
      long minutes = seconds / 60;
      seconds %= 60;
      if (minutes < 60) {
        return minutes + "m " + seconds + "s";
      }
  
      long hours = minutes / 60;
      minutes %= 60;
      if (hours < 24) {
        return hours + "h " + minutes + "m " + seconds + "s";
      }
  
      final long days = hours / 24;
      hours %= 24;
      return days + "d " + hours + "h " + minutes + "m";
    }
docs: Format time period pair to Duration String
  
  @param startTime start time
  @param endTime end time
  @return Duration String