## Trending News

# Java program to convert seconds to HH:MM:SS?

I need a real basic java code to have the user input the seconds and convert it to HH:MM:SS. I just started java and I am having a rough time.

### 2 Answers

- 1 decade agoFavorite Answer
Hello David,

Let me just begin by saying Java is a great programming language!

Okay, let's get down to business. The first thing we always need to do before writing the code is figuring out what we want to do first. As you said, we will take in x amount of seconds and we need to convert them and display that in HH:MM:SS format.

The easiest way is to use the two divide methods / and %.

A recap: using /, like in this context int a = 4/3; our variable a will only have the value of 1. It only takes the whole number.

using % in the same context int a = 4%3; variable a will have a reminding value of 1. This will only take the remainder value.

So, with this knowledge we can divide the total seconds by 3600 to get hours, then take the remainder of 3600 and divide that by 60 to get minutes, and get the same remainder's remainder to get seconds. Our last step is to figure out if our number is less than 10, if it is, add a 0 to it. Let me express this programmatically:

static string formatIntoHHMMSS(secsIn)

{

int hours = secsIn / 3600,

remainder = secsIn % 3600,

minutes = remainder / 60,

seconds = remainder % 60;

return ( (hours < 10 ? "0" : "") + hours

+ ":" + (minutes < 10 ? "0" : "") + minutes

+ ":" + (seconds< 10 ? "0" : "") + seconds );

}

Just in case you don't already know, the last part concatenates hours minutes and seconds into a string, but it also uses discreet form of if clause to check if it's value is less than 10, if it is ten then add a 0 to it, otherwise return it as is.

It consists of a comparison, then a return value if true separated by a colon, then the false return value.

int KellysAge = 16;

string Comment = (KellysAge > 21 ? "Yes, She Can Drink" : "No She Can't");

This is a function, so you can paste it below your main function and call it into your main function.

NOTICE: if you need a standalone console example that you can just slap on, I've written a code at the bottom under sources for your purpose.

Email me (in my profile) if you need further explanation.

I hope this helps, good luck.

Source(s): ~~~~~~~~~~~~~~~~~~~~ import java.io.*; public class FormatTime { public static void main(String[] args) { //this will fetch our input values BufferedReader usrInput = new BufferedReader( new InputStreamReader( System.in) ); System.out.println("Enter a number in seconds to format: "); int secs = usrInput.readLine(); int hours = secs / 3600, remainder = secs % 3600, minutes = remainder / 60, seconds = remainder % 60; string disHour = (hours < 10 ? "0" : "") + hours, disMinu = (minutes < 10 ? "0" : "") + minutes , disSec = (seconds < 10 ? "0" : "") + seconds ; System.out.println(disHour +":"+ disMinu+":"+disSec); } } - quietfredLv 51 decade ago
First thing you have to do is be able to step by step describe the calculation - then worry about coding

so the step by step part is:

int a = user input

int b = a/3600 = hours since 3600 seconds in 1 hr

remark remaining seconds =mod (a-3600)

remainder=mod(a-3600)

int c= remainder / 60 = min since 60 sec = 1 min

remark remaining seconds = mod(remainder/60)

remainder = mod( remainder /60) = seconds

print a + " seconds = " + b + " hours " + c + " minutes " + remainder + " seconds "

hope this helps