Learn Java Coding

Everything You Have To Know About Java

Convert Seconds to Hours Minutes Seconds

If you want to convert seconds to hours:minutes:seconds then could you make difficult calculations with BigDecimal. But you could also use some basic computations to get the right answer.

package P;

import java.io.PrintStream;
import java.util.Scanner;

public class SecToHours {
	PrintStream out;
	Scanner in;
	
	SecToHours() {
		out = new PrintStream(System.out);
		in = new Scanner(System.in);
	}
	
	void start() {
		out.print("Seconds:");
		int totalSecs = in.nextInt();		

		int hours = totalSecs / 3600;
		int minutes = (totalSecs % 3600) / 60;
		int seconds = totalSecs % 60;

		out.printf("%02d:%02d:%02d", hours, minutes, seconds);
	}
	
	public static void main(String[] args) {
		new SecToHours().start();

	}

}

Example:
input: 3600
output: 01:00:00

In this example are we using the scanner method, therefore, you can fill in yourself, a certain number of seconds.

If you want to see more converting programs, click here.

Next Post

Previous Post

© 2019 Learn Java Coding