Convert seconds value to hours minutes seconds?
Solution 1
You should have more luck with
hours = roundThreeCalc.divide(var3600, BigDecimal.ROUND_FLOOR);
myremainder = roundThreeCalc.remainder(var3600);
minutes = myremainder.divide(var60, BigDecimal.ROUND_FLOOR);
seconds = myremainder.remainder(var60);
This will drop the decimal values after each division.
Edit: If that didn't work, try this. (I just wrote and tested it)
public static int[] splitToComponentTimes(BigDecimal biggy)
{
long longVal = biggy.longValue();
int hours = (int) longVal / 3600;
int remainder = (int) longVal - hours * 3600;
int mins = remainder / 60;
remainder = remainder - mins * 60;
int secs = remainder;
int[] ints = {hours , mins , secs};
return ints;
}
Solution 2
Is it necessary to use a BigDecimal? If you don't have to, I'd use an int or long for seconds, and it would simplify things a little bit:
hours = totalSecs / 3600;
minutes = (totalSecs % 3600) / 60;
seconds = totalSecs % 60;
timeString = String.format("%02d:%02d:%02d", hours, minutes, seconds);
You might want to pad each to make sure they're two digit values(or whatever) in the string, though.
Solution 3
DateUtils.formatElapsedTime(long)
, formats an elapsed time in the form "MM:SS
" or "H:MM:SS
" . It returns the String you are looking for. You can find the documentation here
Solution 4
Something really helpful in Java 8
import java.time.LocalTime;
private String ConvertSecondToHHMMSSString(int nSecondTime) {
return LocalTime.MIN.plusSeconds(nSecondTime).toString();
}
Solution 5
Here is the working code:
private String getDurationString(int seconds) {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
seconds = seconds % 60;
return twoDigitString(hours) + " : " + twoDigitString(minutes) + " : " + twoDigitString(seconds);
}
private String twoDigitString(int number) {
if (number == 0) {
return "00";
}
if (number / 10 == 0) {
return "0" + number;
}
return String.valueOf(number);
}
rabbitt
Student, studying software design - mainly focused around VB.net, tiniest amount of Android experience. Interested in WP7 development.
Updated on March 28, 2022Comments
-
rabbitt about 2 years
I've been trying to convert a value of seconds (in a BigDecimal variable) to a string in an editText like "1 hour 22 minutes 33 seconds" or something of the kind.
I've tried this:
String sequenceCaptureTime = ""; BigDecimal roundThreeCalc = new BigDecimal("0"); BigDecimal hours = new BigDecimal("0"); BigDecimal myremainder = new BigDecimal("0"); BigDecimal minutes = new BigDecimal("0"); BigDecimal seconds = new BigDecimal("0"); BigDecimal var3600 = new BigDecimal("3600"); BigDecimal var60 = new BigDecimal("60");
(I have a roundThreeCalc which is the value in seconds so I try to convert it here.)
hours = (roundThreeCalc.divide(var3600)); myremainder = (roundThreeCalc.remainder(var3600)); minutes = (myremainder.divide(var60)); seconds = (myremainder.remainder(var60)); sequenceCaptureTime = hours.toString() + minutes.toString() + seconds.toString();
Then I set the editText to sequnceCaptureTime String. But that didn't work. It force closed the app every time. I am totally out of my depth here, any help is greatly appreciated. Happy coding!
-
Richard Schneider almost 13 yearspossible duplicate of How to convert Milliseconds to "X mins, x seconds" in Java?
-
Ted Hopp almost 13 yearsAny reason why you are using BigDecimal instead of BigInteger?
-
rabbitt almost 13 yearsI will have to implement fractions of a second later on in dev, right now I am just trying to get the calculation to work in the first place.
-
Ben J almost 13 yearsI second Richard's comment - you can use the TimeUnit enum to do a lot of the work for you. developer.android.com/reference/java/util/concurrent/…
-
rabbitt almost 13 yearsHow would I go about using timeunit to convert from a BigDecimal with seconds in it to HHMMSS?
-
-
rabbitt almost 13 yearsI will have to implement fractions of a second later on in dev, right now I am just trying to get the calculation to work in the first place.
-
Alex Kucherenko over 11 yearsThis solution is more graceful: stackoverflow.com/questions/625433/…
-
Hakem Zaied over 11 yearsI like this answer but you need to change '% 10' to '/ 10'
-
Jeffrey Blattman about 10 years
String.format("%02d:%02d:%02d", hours, minutes, seconds);
-
2Dee almost 10 yearsYou could further improve your answer by explaining what the code does and how it solves the problem ;-)
-
Nabin about 8 yearsThis one is simple and exactly what I need. I recommend others this method
-
PSo over 7 yearsThis is simply impressive. I wonder how modulus could work in this way.
-
Pratik Butani over 7 yearsUse Locale otherwise it will gives you warning.
String.format(Locale.ENGLISH, "%02d:%02d:%02d", hours, minutes, seconds)
-
Bob over 7 years@AlexKucherenko except that solution works with milliseconds
-
Hossein Kurd over 6 yearsshortest Answer , Awesome
-
Amir Omidi about 6 years@Mikhail TimeUnit.#### will allow you to do it with any unit of time.
-
karthi over 5 yearsNote: The calculation wraps around midnight when using plusSeconds(long seconds), for eg: if seconds is 86400(24 hrs) it outputs 00:00.
-
Epicurist over 5 yearsIt does exactly what the name suggests 'ConvertSecondToHHMMSSString'. The other (tedious) solutions are also wrapping around.
-
kiranking about 5 yearsDon't forget that it expect parameter in seconds. So you need to convert millis to seconds
-
Paulo Oliveira over 4 years@Epicurist this was the solution I was looking for! Much more elegant and than all the others! :)
-
Epicurist about 4 yearsWhy this infant way using Double typed variables to store the necessary ints?
-
dovetalk about 4 yearsWhenever you find yourself doing math with time and date values, you need to stop yourself and find the right API in the language at hand. If you just need a HH:MM:SS-type response, you'll be better off with
DateUtils.formatElapsedTime
... -
dovetalk about 4 yearsWhenever you find yourself doing math with time and date values, you need to stop yourself and find the right API in the language at hand. If you just need a HH:MM:SS-type response, you'll be better off with
DateUtils.formatElapsedTime
... -
dovetalk about 4 yearsWhenever you find yourself doing math with time and date values, you need to stop yourself and find the right API in the language at hand. If you just need a HH:MM:SS-type response, you'll be better off with
DateUtils.formatElapsedTime
... -
Geobits about 4 years@dovetalk That can definitely be useful, and was added as a separate answer several years ago.
-
dovetalk about 4 years@Geobits, yes, and thanks for linking to it. I spammed many of the "solutions" here with that comment to raise awareness to less experienced developers that are likely to be tempted to roll-their-own. There are some domains where that is generally a bad idea. Time arithmetic is one
-
Geobits about 4 years@dovetalk In general yes, but seconds/minutes are mostly safer than days/years by a long shot. As usual, a lot of ways to do a simple thing.
-
D-Klotz over 3 yearsthis should be the accepted answer as it is cleaner and the hours calculation will show the actual total hours instead of a % mod value of hours.
-
Bruno Bieri over 2 yearsThe provided code won't work as I would expect if you have more seconds than 24 hours.
-
Lahiru Chandima almost 2 yearsWouldn't work when hour count exceeds 24