Method to Convert seconds into Hours, minutes in Kotlin & Java

In this article, we will learn a method to Convert seconds into Hours, minutes in Kotlin & Java. If you want to convert seconds to hours, minutes, and seconds, you can do difficult calculations using BigDecimal. But you can also use some basic calculations to get the correct answer.

Method to Convert seconds

Java Code

Below code is a Java program that converts seconds into days, hours, minutes and seconds

import java.util.Scanner;
 
public class Seconds {
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Enter the number of seconds: ");
      int input = sc.nextInt();
 
      int hours = input / 3600;
      int minutes = (input % 3600) / 60;
      int seconds = (input % 3600) % 60;
 
      System.out.println("Hours: " + hours);
      System.out.println("Minutes: " + minutes);
      System.out.println("Seconds: " + seconds);
 
   }
}

Reference for Java code stack overflow.

Output

$ java Seconds
Enter the number of seconds: 50391
Hours: 13
Minutes: 59
Seconds: 51

Another Code Java/Kotlin

First consider the following value

1 Minute = 60 Seconds
1 Hour = 3600 Seconds ( 60 * 60 )
1 Day = 86400 Second ( 24 * 3600 )
  • First, we have to divide the input by 86400, then this is the number of days if you can get a number greater than 0.
  • Now again divide the remained number by 3600 for the first calculation, this will give you the number of hours.
  • After that divide the remainder of the second calculation by 60 this will give you the number of Minutes.
  • The remaining number from your third calculation is the number of seconds.
int input=500000;
int numberOfDays;
int numberOfHours;
int numberOfMinutes;
int numberOfSeconds;

numberOfDays = input / 86400;
numberOfHours = (input % 86400 ) / 3600 ;
numberOfMinutes = ((input % 86400 ) % 3600 ) / 60 
numberOfSeconds = ((input % 86400 ) % 3600 ) % 60  ;

This is the Method to Convert seconds into Hours, minutes in Kotlin & Java

Feel free to ask your questions or give suggestions in the comments section below.

Next Article

1. Convert Milliseconds to Minutes and Seconds Using Java
2. Convert Milliseconds to Minutes and Seconds Using Kotlin

Please share this post:
Posts created 31

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: