Chapter VI Question 25 (Convert milliseconds to hours, minutes, and seconds)

Chapter VI Question 25 (Convert milliseconds to hours, minutes, and seconds)

  • **6.25 (Convert milliseconds into hours, minutes, and seconds) Use the method header below to write a method to convert milliseconds into hours, minutes, and seconds.
    public static String convertMillis(long millis)
    This method returns a string of the form "hour:minute:second". For example: convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Write a test program that prompts the user to enter a long millisecond and displays a string in the format of "hour: minute: second".
    **6.25(Convert milliseconds to hours, minutes, and 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. For example, convertMillis(5500) returns a string 0:0:5, convertMillis(100000)returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
  • Reference Code:
package chapter06;

import java.util.Scanner;

public class Code_25 {
    
    
    public static void main(String[] args) {
    
    
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("enter a long integer for milliseconds: ");
        long millis = inputScanner.nextLong();
        System.out.printf("The time is %s", convertMillis(millis));
    }
    public static String convertMillis(long millis) {
    
    
        String timeString = "";
        long totalSeconds = millis / 1000;
        long currentSecond = totalSeconds % 60;
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
        long totalHours = totalMinutes / 60;
        timeString = String.valueOf(totalHours) + ":" + String.valueOf(currentMinute) + ":" + String.valueOf(currentSecond);
        return timeString;
    }
}

  • The results show that:
enter a long integer for milliseconds: 555550000
The time is 154:19:10
Process finished with exit code 0

Guess you like

Origin blog.csdn.net/jxh1025_/article/details/109169546