Java Program to Convert Hour to Day and Day to Hour

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

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

Java Program to Convert Hour to Day and Day to Hour

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

Hour is used as unit of time and Day is a period of 24 hours starting from midnight 12 AM to next 12AM which is nothing but 24 hours.

1 Hour = 0.0416667 Day
1 Day =  24 Hour

Formula to convert Day to Hour.

Hour = Day * 24

Formula to convert Hour to Day.

Day = Hour / 24

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

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

Approach:

  • Declare Hour and Day value.
  • Then convert Hour to Day and Day 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 day declared 
        double day = 1;

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

Value of 1.0 hour in day: 0.041666666666666664
Value of 1.0 day in hour: 24.0

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

Approach:

  • Take user input of Hour and Day value.
  • Then convert Hour to Day and Day 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 day
        System.out.println("Enter value of day: ");  
        double day = sc.nextDouble();

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

Enter value of hour: 
5
Enter value of day: 
2
Value of 5.0 hour in day: 0.20833333333333334
Value of 2.0 day in hour: 48.0

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

Approach:

  • Take user input of Hour and Day value.
  • Call a user defined method by passing Hour and Day value as parameter.
  • Inside method convert Hour to Day and Day 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 day
        System.out.println("Enter value of day: ");  
        double day = sc.nextDouble();
         //calling user defined method convert()
        convert(hour, day);
   }
   
   //convert() method to convert day to hour and vice versa
   public static void convert(double hour, double day)
   {
        //converting hour to day
        double d = hour / 24;
        //converting day to hour 
        double hr = day * 24;
        //printing result
        System.out.println("Value of "+hour+" hour in day: "+ d);   
        System.out.println("Value of "+day+" day in hour: "+ hr);   
   }
}
Output:

Enter value of hour: 
340
Enter value of day: 
2.5
Value of 340.0 hour in day: 14.166666666666666
Value of 2.5 day in hour: 60.0

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: