X milliseconds = X / 1000 seconds = (X / 1000) / 60 minutes
If you have 100,000 milliseconds, divide this value by 1,000 and you're left with 100 seconds. Now 100 / 60 = 1.666~ minutes, but fractional minutes have no value, so: do 100 % 60 = 40 seconds to find the remainder, then integer division 100 / 60 = 1 minute, with 40 seconds remainder. Answer: 1 minute, 40 seconds.
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);
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.
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.
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);
}
This is related to a previous post, but in my opinion the solution proposed wasn't quite right.
In order to realize a correct conversion, this is what should be implemnted:
long time_millis = 1926546
int minutes = time_millis / 1000 / 60
int seconds = ((int)(time_millis / 1000) % 60) #important that this division is cast to an int
println "Build time: $minutes minutes $seconds seconds"