Java Program to Convert Hour to Second and Second to Hour

In the previous article, we have discussed about Java Program to Convert Millisecond to Microsecond and Microsecond to Millisecond

In this article we will see how to convert Hour to Second and Second to Hour by using Java programming language.

Java Program to Convert Hour to Second and Second to Hour

Before jumping into the program let’s know the relationship between Hour and Second and how we can convert Hour to Second and vice versa.

Hour and Second are used as unit of time.

1 Hour = 3600 Second
1 Second = 0.000277778 Hour

Formula to convert Second to Hour.

Hour = Second / 3600

Formula to convert Hour to Second.

Second = Hour * 3600

Let’s see different ways to convert Hour to Second and Second to Hour.

Method-1: Java Program to Convert Hour to Second and Second to Hour By Using Static Input Value

Approach:

  • Declare Hour and Second value.
  • Then convert Hour to Second and Second to Hour by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //value of hour declared
        double hour = 1;
        //value of second declared 
        double second = 1;

        //converting hour to second
        double sec = hour*3600;
        //converting second to hour 
        double hr = second/3600;
        //printing result
        System.out.println("Value of "+hour+" hour in second: "+ sec);   
        System.out.println("Value of "+second+" second in hour: "+ hr);   
   }
}
Output:

Value of 1.0 hour in second: 3600.0
Value of 1.0 second in hour: 2.777777777777778E-4

Method-2: Java Program to Convert Hour to Second and Second to Hour By Using User Input Value

Approach:

  • Take user input of Hour and Second value.
  • Then convert Hour to Second and Second to Hour by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable hour
        System.out.println("Enter value of hour: ");  
        double hour = sc.nextDouble();
        //Taking the value input of double variable second
        System.out.println("Enter value of second: ");  
        double second = sc.nextDouble();

        //converting hour to second
        double sec = hour*3600;
        //converting second to hour 
        double hr = second/3600;
        //printing result
        System.out.println("Value of "+hour+" hour in second: "+ sec);   
        System.out.println("Value of "+second+" second in hour: "+ hr);   
   }
}
Output:

Enter value of hour: 
2
Enter value of second: 
5000
Value of 2.0 hour in second: 7200.0
Value of 5000.0 second in hour: 1.3888888888888888

Method-3: Java Program to Convert Hour to Second and Second to Hour By Using User Defined Method

Approach:

  • Take user input of Hour and Second value.
  • Call a user defined method by passing Hour and Second value as parameter.
  • Inside method convert Hour to Second and Second to Hour by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable hour
        System.out.println("Enter value of hour: ");  
        double hour = sc.nextDouble();
        //Taking the value input of double variable second
        System.out.println("Enter value of second: ");  
        double second = sc.nextDouble();
        //calling user defined method convert()
        convert(second, hour);
   }
   
   //convert() method to convert second to hour and vice versa
   public static void convert(double second, double hour)
   {
        //converting hour to second
        double sec = hour*3600;
        //converting second to hour 
        double hr = second/3600;
        //printing result
        System.out.println("Value of "+hour+" hour in second: "+ sec);   
        System.out.println("Value of "+second+" second in hour: "+ hr);   
   }
}
Output:

Enter value of hour: 
3.5
Enter value of second: 
9000
Value of 3.5 hour in second: 12600.0
Value of 9000.0 second in hour: 2.5

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: