Convert seconds to hour, minute and seconds in Java
Input - Question 17
In this question, we will see how to input total time in seconds in Java programming using the java input and print the time in hours, minutes and seconds. To know more about java input click on the java input lesson.
Q17) Write a program in Java to input total time in seconds and print the time in hours, minutes and seconds.
Total Seconds: 4206
Hour: 1
Minutes: 10
Seconds: 6
Program
import java.util.Scanner;
public class Q17
{
public static void main(String args[])
{
int h,m,s,ts;
Scanner sc=new Scanner(System.in);
System.out.print("Enter total seconds ");
ts=sc.nextInt();
h=ts/3600;
m=(ts%3600)/60;
s=(ts%3600)%60;
System.out.println("Hours=" + h);
System.out.println("Minutes=" + m);
System.out.print("Seconds=" + s);
}
}
Output
Enter total seconds 4830 Hours=1 Minutes=20 Seconds=30