Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Milliseconds to Minutes and Seconds?

People also ask

How do you convert milliseconds to seconds?

To convert a millisecond measurement to a second measurement, divide the time by the conversion ratio. The time in seconds is equal to the milliseconds divided by 1,000.

How do you find the hours and minutes from milliseconds?

To convert milliseconds to hours, minutes, seconds:Divide the milliseconds by 1000 to get the seconds. Divide the seconds by 60 to get the minutes. Divide the minutes by 60 to get the hours. Add a leading zero if the values are less than 10 to format them consistently.

How do you convert milliseconds?

The time in milliseconds is equal to the seconds multiplied by 1,000.


I would suggest using TimeUnit. You can use it like this:

long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

After converting millis to seconds (by dividing by 1000), you can use / 60 to get the minutes value, and % 60 (remainder) to get the "seconds in minute" value.

long millis = .....;  // obtained from StopWatch
long minutes = (millis / 1000)  / 60;
int seconds = (int)((millis / 1000) % 60);

tl;dr

Duration d = Duration.ofMillis( … ) ;
int minutes = d.toMinutesPart() ;
int seconds = d.toSecondsPart() ;

Java 9 and later

In Java 9 and later, create a Duration and call the to…Part methods. In this case: toMinutesPart and toSecondsPart.

Capture the start & stop of your stopwatch.

Instant start = Instant.now(); 
…
Instant stop = Instant.now();

Represent elapsed time in a Duration object.

Duration d = Duration.between( start , stop );

Interrogate for each part, the minutes and the seconds.

int minutes = d.toMinutesPart();
int seconds = d.toSecondsPart();

You might also want to see if your stopwatch ran expectedly long.

Boolean ranTooLong = ( d.toDaysPart() > 0 ) || ( d.toHoursPart() > 0 ) ;

Java 8

In Java 8, the Duration class lacks to…Part methods. You will need to do math as shown in the other Answers.

long entireDurationAsSeconds = d.getSeconds();

Or let Duration do the math.

long minutesPart = d.toMinutes(); 
long secondsPart = d.minusMinutes( minutesPart ).getSeconds() ;

See live code in IdeOne.com.

Interval: 2016-12-18T08:39:34.099Z/2016-12-18T08:41:49.099Z

d.toString(): PT2M15S

d.getSeconds(): 135

Elapsed: 2M 15S

Resolution

FYI, the resolution of now methods changed between Java 8 and Java 9. See this Question.

  • Java 9 captures the moment with a resolution as fine as nanoseconds. Resolution depends on capability of your computer’s hardware. I see microseconds (six digits of decimal fraction) on MacBook Pro Retina with macOS Sierra.
  • Java 8 captures the moment only up to milliseconds. The implementation of Clock is limited to a resolution of milliseconds. So you can store values in nanoseconds but only capture them in milliseconds.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

Table of which java.time library to use with which version of Java or Android


I was creating a mp3 player app for android, so I did it like this to get current time and duration

 private String millisecondsToTime(long milliseconds) {
    long minutes = (milliseconds / 1000) / 60;
    long seconds = (milliseconds / 1000) % 60;
    String secondsStr = Long.toString(seconds);
    String secs;
    if (secondsStr.length() >= 2) {
        secs = secondsStr.substring(0, 2);
    } else {
        secs = "0" + secondsStr;
    }

    return minutes + ":" + secs;
}

This is just basic math. 1000 milliseconds=1 second and 60000 milliseconds = 1 minute; So just do,

int seconds=(millis/1000)%60;

long minutes=((millis-seconds)/1000)/60;

To convert time in millis directly to minutes: second format you can use this

String durationText = DateUtils.formatElapsedTime(timeInMillis / 1000));

This will return a string with time in proper formatting. It worked for me.


  public static String getIntervalTime(long longInterval) {
    
    long intMillis = longInterval;
    long dd = TimeUnit.MILLISECONDS.toDays(intMillis);
    long daysMillis = TimeUnit.DAYS.toMillis(dd);
    intMillis -= daysMillis;
    long hh = TimeUnit.MILLISECONDS.toHours(intMillis);
    long hoursMillis = TimeUnit.HOURS.toMillis(hh);
    intMillis -= hoursMillis;
    long mm = TimeUnit.MILLISECONDS.toMinutes(intMillis);
    long minutesMillis = TimeUnit.MINUTES.toMillis(mm);
    intMillis -= minutesMillis;
    long ss = TimeUnit.MILLISECONDS.toSeconds(intMillis);
    long secondsMillis = TimeUnit.SECONDS.toMillis(ss);
    intMillis -= secondsMillis;

    String stringInterval = "%02d days - %02d:%02d:%02d.%03d";
    return String.format(stringInterval , dd, hh, mm, ss, intMillis);
  }

Shorter Form!

  public static String getIntervalTime(long longInterval) {

    long intMillis = longInterval;
    long dd = TimeUnit.MILLISECONDS.toDays(intMillis);
    intMillis -= TimeUnit.DAYS.toMillis(dd);
    long hh = TimeUnit.MILLISECONDS.toHours(intMillis);
    intMillis -= TimeUnit.HOURS.toMillis(hh);
    long mm = TimeUnit.MILLISECONDS.toMinutes(intMillis);
    intMillis -= TimeUnit.MINUTES.toMillis(mm);
    long ss = TimeUnit.MILLISECONDS.toSeconds(intMillis);
    intMillis -= TimeUnit.SECONDS.toMillis(ss);

    String stringInterval = "%02d days - %02d:%02d:%02d.%03d";
    return String.format(stringInterval , dd, hh, mm, ss, intMillis);
  }

Testing

long delay = 1000*60*20 + 1000*5 + 10;
LOGGER.log(Level.INFO, "Delay Expected {0}", getIntervalTime(delay));

Output

INFO: Delay Expected 00 days - 00:20:05.010