0

check my function below, its showing like this 124:45 *means it is not converting hours into minutes. Like this:

enter image description here

Kindly make changes in my below function that give me correct time.

    private String formatedTime(int mCuurentPosition) {
        String totalout="";
        String totalnew="";
        String seconds=String.valueOf(mCuurentPosition % 60);
        String mints =String.valueOf(mCuurentPosition / 60);
        totalout =mints + ":" + seconds;
        totalnew=mints + ":" + "0" + seconds;
        if (seconds.length()==1){
            return totalnew;
        }else {
            return totalout;

        }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Maybe you can piece a solution together from the following existing questions? (1) [Convert number of seconds into HH:MM (without seconds) in Java](https://stackoverflow.com/questions/50096083/convert-number-of-seconds-into-hhmm-without-seconds-in-java) (2) [How can I “pretty print” a Duration in Java?](https://stackoverflow.com/questions/3471397/how-can-i-pretty-print-a-duration-in-java) (3) [How do i format a java.time.Duration mm:ss](https://stackoverflow.com/questions/28675095/how-do-i-format-a-java-time-duration-mmss). Search for still more. – Ole V.V. Mar 30 '21 at 17:36
  • And next time search *before* you post your question, please. It’s easier for you, and it’s easier for us. – Ole V.V. Mar 30 '21 at 17:36
  • **Finally this function work for me** `public String formatedTime(int ms){ String tim = ""; String secStrng = ""; String minStrng=""; int s = (int) (ms % 60); int m = (int)(ms / 60) % 60; int h = (int)(( ms / (60 * 60)) % 24 ); if(h > 0){ tim = h + ":"; } if(s < 10){ secStrng = "0" + s; }else{ secStrng = "" + s; } if (m < 10){ minStrng = "0" + m; }else { minStrng = "" + m; } tim = tim + minStrng + ":" + secStrng; return tim; }` – Aamir Mehmood Lucky Apr 04 '21 at 15:05
  • Thanks for posting your method. It’s way too much “hand work” for my taste. – Ole V.V. Apr 04 '21 at 15:19

1 Answers1

0

java.time.Duration, desugaring and String.format()

private String formattedTime(int mCurrentPosition) {
    Duration pos = Duration.ofSeconds(mCurrentPosition);
    long hours = pos.toHours();
    if (hours == 0) {
        return String.format("%d:%02d", pos.toMinutes(), pos.toSecondsPart());
    } else {
        return String.format("%d:%02d:%02d",
                hours, pos.toMinutesPart(), pos.toSecondsPart());
    }
}

I haven’t got an Android development environment with me, so am not 100 % sure that desugaring can handle this, but I would certainly dare hope so. The toMinutesPart and toSecondsPart methods were introduced in Java 9. String.format() has been around since Java 1.5, so in any case let it do the formatting for you.

See the above method in action:

    System.out.println(formattedTime(47));
    System.out.println(formattedTime(4747));

Output is:

0:47
1:19:07

Question: Doesn’t java.time.Duration require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Similar questions (no strict duplicates, but loads of helpful information):

Other links:

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • **this work for me** `public String formatedTime(int ms){ String tim = ""; String secStrng = ""; String minStrng=""; int s = (int) (ms % 60); int m = (int)(ms / 60) % 60; int h = (int)(( ms / (60 * 60)) % 24 ); if(h > 0){ tim = h + ":"; } if(s < 10){ secStrng = "0" + s; }else{ secStrng = "" + s; } if (m < 10){ minStrng = "0" + m; }else { minStrng = "" + m; } tim = tim + minStrng + ":" + secStrng; return tim; }` – Aamir Mehmood Lucky Apr 04 '21 at 15:06
  • its show bad here,,because i don't know how to comment properly here,,,but this function work **100% work perfectly** actully i was show media player current time in textview, that time in miliseconds and this function convert miliseconds to Hour:min:sec if hour not exist then it will show only mintues and seconds, **MUST TRY THIS FUNCTION** – Aamir Mehmood Lucky Apr 04 '21 at 15:09
  • There is no way to format code in comments here on Stack Overflow. I have pasted your method into my IDE, and it has formatted it for me (don’t expect other readers to do the same). – Ole V.V. Apr 04 '21 at 15:22