Write a method that converts milliseconds to hours, minutes, and seconds using the following header: public static String convertMillis(long millis) The method returns a string as hours:minutes:seconds.

Write a method that converts milliseconds to hours, minutes, and seconds using the following header:

public static String convertMillis(long millis)

The method returns a string as hours:minutes:seconds.


Program:

import java.util.*;
public class ProgrammingExercise6_25 {

 public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long millis;
String output;
System.out.println("Enter milliseconds: ");
millis = input.nextLong();
convertMillis(millis);
}

public static void convertMillis(long millis) {
long secs = (millis / 1000)%60;
    long min = ((millis / 1000) / 60)%60;
    long hrs = ((millis / 1000) / 60)/ 60;

 System.out.println( hrs + ":" + min + ":" + secs);
}
}



Result:
Enter milliseconds:  555550000
154:19:10

No comments:

Post a Comment