Convert milliseconds to readable string : Time « Development Class « Java

Convert milliseconds to readable string

    Copyright (C) 2006 The Lobo Project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Contact info:

public class Timing {
  public static double round1(double value) {
    return Math.round(value * 10.0) / 10.0;
  public static String getElapsedText(long elapsedMillis) {
    if(elapsedMillis < 60000) {
      double unit = round1(elapsedMillis / 1000.0); 
      return unit + (unit == 1 ? " second" : " seconds");
    else if(elapsedMillis < 60000 * 60) {
      double unit = round1(elapsedMillis / 60000.0); 
      return unit + (unit == 1 ? " minute" : " minutes");
    else if(elapsedMillis < 60000 * 60 * 24) {
      double unit = round1(elapsedMillis / (60000.0 * 60)); 
      return unit + (unit == 1 ? " hour" : " hours");
    else {
      double unit = round1(elapsedMillis / (60000.0 * 60 * 24)); 
      return unit + (unit == 1 ? " day" : " days");


Related examples in the same category

1.Get Time From Date
2.ISO8601 Date Time Format
3.Time Format
4.Time Formatter
5.Returns time string
6.Returns the given date with the time values cleared
7.Convert the time to the midnight of the currently set date
8.Compare both times and dates
9.Tells you if the date part of a datetime is in a certain time range
10.Returns the given date with time set to the end of the day
11.Determines whether or not a date has any time values (hour, minute, seconds or millisecondsReturns the given date with the time values cleared
12.Returns a formatted String from time
13.Time library
14.Elapsed time in hours/minutes/seconds
15.Sets the time on the same day to 00:00:00.000
16.Determines if given times span at least an entire day
17.Converts a given time in milliseconds into a XMLGregorianCalendar object.
18.Time Distance
19.Time Formatter
20.Format time
21.A utility class for representing a span of time.
22.GmtCalendar is a useful class for working with times that are based using GMT time.
23.Time Period
24.Represents the TSTInfo strcture within a time-stamp token (RFC 3161).
25.SimpleTimer enables bounded and unbounded waits.
26.Takes a time in milliseconds and returns an hours, minutes and seconds representation.
27.Format time in milliseconds