Start learning free

LESSON 21 OF 410 · CALCULATIONS WITH NUMBERS

Convert Hours and Minutes into Seconds in Java

Lesson Read the full explanation

We created two variables to store the number of hours and minutes we worked on a project. We want to convert this amount of time into seconds.

Use these two variables and some arithmetic operations to convert and print to the console the number of seconds the hours and minutes add up to.


Example:

For example, if hours was 1 and minutes was 2, the answer would be 3720.

Why? Because one hour and two minutes add up to 3720 seconds.


Need a hint?


1 hour is made up of 60 minutes. To how many minutes do hours and minutes add up?

hours and minutes add up to hours * 60 + minutes minutes.

Now, how many seconds are there in hours * 60 + minutes minutes?



As 1 minute is made up of 60 seconds, and we have (hours * 60 + minutes) minutes, the answer is: (hours * 60 + minutes) * 60


STEP 1 OF 1
    Ctrl + Enter to check

    JAVA · CALCULATIONS WITH NUMBERS

    Convert Hours And Mins Into Seconds in Java, explained

    The task and the formula

    The exercise starts with two int variables, hours set to 13 and minutes set to 47. The goal is to print how many seconds that amount of time adds up to. For 1 hour and 2 minutes the answer is 3720, because 62 minutes times 60 seconds is 3720.

    The conversion has two stages, since there are 60 minutes in an hour and 60 seconds in a minute:

    1. Hours to minutes: hours * 60, then add the loose minutes. That is the total number of minutes.
    2. Minutes to seconds: multiply that total by 60.

    The general formula for converting hours to seconds is hours * 3600, since an hour has 3600 seconds, and one minute is 60 seconds. In one expression for both: (hours * 60 + minutes) * 60. With 13 and 47: 13 * 60 is 780, plus 47 is 827 minutes, and 827 * 60 is 49,620 seconds.

    Why the parentheses matter

    Java follows the usual precedence rules: multiplication and division run before addition and subtraction, and operators of the same level run left to right. Without parentheses, hours * 60 + minutes * 60 would multiply minutes by 60 but leave hours counted as minutes, giving 780 + 2820 = 3600 instead of 49,620. Writing hours * 60 + minutes inside parentheses forces the addition to happen first, so the final * 60 applies to the whole sum.

    An equivalent form skips the parentheses by converting each part separately: hours * 3600 + minutes * 60. Both give 49,620. The first matches the way people think about the problem; the second does fewer operations. Either is a fine answer.

    The solution in Java

    int hours = 13;
    int minutes = 47;
    
    System.out.println((hours * 60 + minutes) * 60);
    

    This prints 49620. The statement gives println a single int expression, so it prints the number followed by a line break.

    Printing with a label

    To print text with the number, concatenate with +. The trap is that + also joins strings, and the order of evaluation decides which meaning applies:

    System.out.println("Seconds: " + (hours * 60 + minutes) * 60);
    System.out.println("Seconds: " + hours * 60 + minutes);
    

    The first line prints Seconds: 49620. The second prints Seconds: 78047: the multiplication runs first and gives 780, but then the string is joined with 780 and after that with 47, as text. When a number must be added before it is joined to a string, put it in parentheses.

    Keeping the steps in variables

    For a longer calculation, store the intermediate results, which also gives each one a name:

    int totalMinutes = hours * 60 + minutes;
    int totalSeconds = totalMinutes * 60;
    System.out.println(totalSeconds);
    

    This prints 49620 as well. Declaring variables costs nothing and makes it clear what each number means, which is useful once formulas get longer.

    Common mistakes

    • Forgetting one factor of 60. hours * 60 + minutes is minutes, not seconds. The result 827 is a sign that the last multiplication is missing.
    • Dropping the parentheses, as shown above, so the final * 60 applies only to minutes.
    • Mixing up the unit. Multiplying hours by 100 or minutes by 100 comes from decimal habits. Time uses 60.
    • Integer overflow with very large values. An int holds up to 2,147,483,647. With hours = 600000, hours * 3600 overflows and prints -2134967296. Using long fixes it: 600000 * 3600L is 2160000000. For the lesson's values (13 and 47) int is plenty.
    • Fractions of an hour. With double hours = 1.5, the same formula works and the result is 5400.0, because the arithmetic is done on doubles.

    Testing

    • hours = 1, minutes = 2 gives 3720.
    • hours = 0, minutes = 0 gives 0.
    • hours = 1, minutes = 0 gives 3600.
    • hours = 0, minutes = 1 gives 60.
    • hours = 10, minutes = 59 gives 39540.
    • hours = 13, minutes = 47 gives 49620.
    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free