Converting Milliseconds to Minutes and Seconds?
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….