-3
import java.util.Scanner;

public class Millls {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

    System.out.println("Enter milliseconds: ");
    long millis= sc.nextLong();

    System.out.println(convertMillis(millis));

    }

    public static String convertMillis(long millis){

        long s = (millis / 1000) % 60;
        long m = (millis / (1000 * 60)) % 60;   
        long hh = (millis / (1000 * 60 * 60)) % 24;
        String time = String.format("%d:%d:%d",hh,m,s);
        return time;
    }
}

I need to specifically do convert mills (555550000) to return a string 154:19:10. Please it is different than the other questions. I had try %02: but it still doesn't work

2 Answers2

0

Refer java.util.concurrent.TimeUnit Api in Java. It had direct methods to convert from one time unit to another.

Sample usage:

    long s = TimeUnit.SECONDS.convert(555550000, TimeUnit.MILLISECONDS);
    long m = TimeUnit.MINUTES.convert(555550000, TimeUnit.MILLISECONDS);
    long hh = TimeUnit.HOURS.convert(555550000, TimeUnit.MILLISECONDS);
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0

You're taking the modulo 24 of the hours. So you'll always end up with hours between 0 and 23 inclusive. Remove the modulo and you'll get the expected result:

long hh = (millis / (1000 * 60 * 60))
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255