hey

I'm trying to write a method that converts milliseconds to hours, minutes, and seconds using the following header:

public static String convertMillis(long millis)

My method needs to return a string as hours:minutes:seconds.

For example, convertMillis(5500) returns a string 0:0:5.

this is what i have so far, can someone please help me.

import java.util.*;
class Millis
{
    public static void main(String[] args) 
    {
    }

    public static String convertMillis(long millis) {

        double hours;
        double minutes;
        double seconds;
        int milliseconds = 0;
        int currentSeconds = 0;
        int totalSeconds = 0;
        int totalMinutes = 0;
        int totalHours = 0;

System.out.print("convertMillis:");
milliseconds = input.nextInt();

        seconds = milliseconds/1000;
        hours = seconds/(60*60);
        minutes = seconds % ((60*60));

    System.out.println("your conversion equals" hours + ":" + minutes + ":" + seconds);
return null;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hope this solves your problem.

public static void convertMillis(int Millis){
        String convert = String.format("You have %d hour(s), %d minute(s), and %d second(s)",
                Millis/(1000*60*60), (Millis%(1000*60*60))/(1000*60), ((Millis%(1000*60*60))%(1000*60))/1000);
        System.out.println(convert);
    }

Edited 6 Years Ago by Zetlin: n/a

Figure it out. I noticed you have a tendency to not mark threads solved or to follow up on your old threads. Anyway, zetlin just gave you the exact method you need from the looks of things. Now you want him to edit it into your code for you?

Actually he pm me with the same question and I was nice enough to show him how to run it using just the class name.

This article has been dead for over six months. Start a new discussion instead.