Java Code Example

This program that asks the user to enter a number of seconds and calculates the total number of hours, minutes, and seconds.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
public class timeMethod{
public static void main(String[] args)
{
long seconds = getSeconds();
String result = format(seconds);
outputTime(seconds, result);
}
public static long getSeconds()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter total seconds: ");
long seconds = input.nextLong();
return seconds;
}
public static String format(long seconds)
{
long seconds1 = (seconds % 3600) % 60;
long minutes = (seconds % 3600) / 60;
long hours = (seconds % 86400) / 3600;

String result = (hours + ":" + minutes + ":" + seconds1);
return result;
}
    
public static void outputTime(long seconds, String result)
{
System.out.println("The hours, minutes, seconds for total seconds " + seconds + "is " + result);
  }}

Back to Page 1

Courses