Convert seconds value to hours minutes seconds?

276,598

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);
}
Share:
276,598
rabbitt
Author by

rabbitt

Student, studying software design - mainly focused around VB.net, tiniest amount of Android experience. Interested in WP7 development.

Updated on March 28, 2022

Comments

  • rabbitt
    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
      Richard Schneider almost 13 years
    • Ted Hopp
      Ted Hopp almost 13 years
      Any reason why you are using BigDecimal instead of BigInteger?
    • rabbitt
      rabbitt almost 13 years
      I 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
      Ben J almost 13 years
      I 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
      rabbitt almost 13 years
      How would I go about using timeunit to convert from a BigDecimal with seconds in it to HHMMSS?
  • rabbitt
    rabbitt almost 13 years
    I 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
    Alex Kucherenko over 11 years
    This solution is more graceful: stackoverflow.com/questions/625433/…
  • Hakem Zaied
    Hakem Zaied over 11 years
    I like this answer but you need to change '% 10' to '/ 10'
  • Jeffrey Blattman
    Jeffrey Blattman about 10 years
    String.format("%02d:%02d:%02d", hours, minutes, seconds);
  • 2Dee
    2Dee almost 10 years
    You could further improve your answer by explaining what the code does and how it solves the problem ;-)
  • Nabin
    Nabin about 8 years
    This one is simple and exactly what I need. I recommend others this method
  • PSo
    PSo over 7 years
    This is simply impressive. I wonder how modulus could work in this way.
  • Pratik Butani
    Pratik Butani over 7 years
    Use Locale otherwise it will gives you warning. String.format(Locale.ENGLISH, "%02d:%02d:%02d", hours, minutes, seconds)
  • Bob
    Bob over 7 years
    @AlexKucherenko except that solution works with milliseconds
  • Hossein Kurd
    Hossein Kurd over 6 years
    shortest Answer , Awesome
  • Amir Omidi
    Amir Omidi about 6 years
    @Mikhail TimeUnit.#### will allow you to do it with any unit of time.
  • karthi
    karthi over 5 years
    Note: The calculation wraps around midnight when using plusSeconds(long seconds), for eg: if seconds is 86400(24 hrs) it outputs 00:00.
  • Epicurist
    Epicurist over 5 years
    It does exactly what the name suggests 'ConvertSecondToHHMMSSString'. The other (tedious) solutions are also wrapping around.
  • kiranking
    kiranking about 5 years
    Don't forget that it expect parameter in seconds. So you need to convert millis to seconds
  • Paulo Oliveira
    Paulo Oliveira over 4 years
    @Epicurist this was the solution I was looking for! Much more elegant and than all the others! :)
  • Epicurist
    Epicurist about 4 years
    Why this infant way using Double typed variables to store the necessary ints?
  • dovetalk
    dovetalk about 4 years
    Whenever 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
    dovetalk about 4 years
    Whenever 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
    dovetalk about 4 years
    Whenever 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
    Geobits about 4 years
    @dovetalk That can definitely be useful, and was added as a separate answer several years ago.
  • dovetalk
    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
    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
    D-Klotz over 3 years
    this 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
    Bruno Bieri over 2 years
    The provided code won't work as I would expect if you have more seconds than 24 hours.
  • Lahiru Chandima
    Lahiru Chandima almost 2 years
    Wouldn't work when hour count exceeds 24