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