In: Computer Science
Write a program that request a time interval in seconds and display it in hours, minutes, second format. (java)
Note: I have added a main method to show sample output and also uploaded the output image.Please comment if you need any clarifiaction.
Java code:
import java.util.Scanner; public class Time { public static void main(String[] args) { System.out.println("Enter time in seconds"); Scanner sc = new Scanner(System.in); System.out.println(secToTime(sc.nextInt())); System.out.println(secToTime(9)); System.out.println(secToTime(120)); System.out.println(secToTime(7240)); } /** * * @param seconds Time in seconds * @return Time in hours:minutes:seconds format */ static String secToTime(int seconds) { //Number of seconds after conversion int sec = seconds % 60; //Number of minutes after conversion int min = seconds / 60; //Number of hours after conversion int hr = 00; if (min >= 60) { hr = min / 60; min %= 60; } String second = String.format("%02d", sec); String minute = String.format("%02d", min); String hour = String.format("%02d", hr); return hour + ":" + minute + ":" + second; } }
Output: