Convert Seconds to Hours and Minutes

Convert total seconds into hours, minutes, and remaining seconds.

BeginnerTopic: Module 1: Basic Java Programs
Back

Java Convert Seconds to Hours and Minutes Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter total seconds: ");
        int totalSeconds = sc.nextInt();

        int hours = totalSeconds / 3600;
        int remaining = totalSeconds % 3600;
        int minutes = remaining / 60;
        int seconds = remaining % 60;

        System.out.println("Time = " + hours + " hour(s) " + minutes + " minute(s) " + seconds + " second(s)");

        sc.close();
    }
}
Output
Enter total seconds: 3672
Time = 1 hour(s) 1 minute(s) 12 second(s)

Understanding Convert Seconds to Hours and Minutes

We repeatedly divide by 3600 and 60 to extract hours, minutes, and remaining seconds.

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents