Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-224

[lang] elapsed time formatting utility method

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.0.1
    • 2.1
    • None
    • None
    • Operating System: All
      Platform: All

    • 15082

    Description

      I haven't seen any public utilities out there that can display an elapsed time
      in a human readable form. I figured if it doesn't already exist in the commons,
      it would be useful for others. I'm not sure what package it would go in
      (StringUtil or NumberUtil maybe?). Anyway, here's a little public static method
      that I use to get a readable elapsed time display for a time in milliseconds.
      Not that this is rocket science, but I found the guts of the code on some public
      forum.

      ---------------------------
      /**

      • Formats an elapsed time in milliseconds to a more human-readable form.
      • Output is in the format hh:mm:ss
      • @param elapsedMillis time in milliseconds.
      • @return the amount of time in hh:mm:ss format.
        */
        public static String formatTime(long elapsedMillis) { // first turn the amount in millis to be seconds...it's easier to format the hhmmss that way. long elapsedSeconds = elapsedMillis / 1000; long formattedHours = elapsedSeconds / 3600; long formattedMinutes = (elapsedSeconds % 3600) / 60; long formattedSeconds = elapsedSeconds % 60; // if you also want formatted milliseconds, here's how... // long formattedMillis = elapsedMillis % 1000; return ((formattedHours < 10 ? "0" : "") + formattedHours + ":" + (formattedMinutes < 10 ? "0" : "") + formattedMinutes + ":" + (formattedSeconds < 10 ? "0" : "") + formattedSeconds); }

        ---------------------------------

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              dbtanner Dan Tanner
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: