Java: Split seconds into days and hours

Integer division and the remainder operator turn one big number into days, hours, minutes and seconds.

What to build

A program that writes a number of seconds in days, hours, minutes and seconds.

Requirements

  • Read a number of seconds (a whole number, possibly larger than an int can hold).
  • Print D days, H hours, M minutes, S seconds.

Input

One whole number.

Output

One line.

Constraints

0 ≤ seconds ≤ 10^15.

Example

Input:

90061

Output:

1 days, 1 hours, 1 minutes, 1 seconds

Why it works

Integer division gives how many whole days fit, and % gives what is left over, so each step works on the remainder of the one before; a long keeps the large values exact.

Examples

Example 1 · Input
90061
Expected output
1 days, 1 hours, 1 minutes, 1 seconds
Example 2 · Input
59
Expected output
0 days, 0 hours, 0 minutes, 59 seconds

Hints

Hint 1

Use a long: the number may be bigger than an int can hold.

Hint 2

Divide by 86400 for days, and use % to keep what is left.

Solution

Show one correct solution (try it yourself first)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long s = in.nextLong();
        long days = s / 86400;
        long hours = s % 86400 / 3600;
        long minutes = s % 3600 / 60;
        long seconds = s % 60;
        System.out.println(days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds");
    }
}

Lesson: Variables and types →