Java Program to Convert Hour to Week and Week to Hour

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

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

Java Program to Convert Hour to Week and Week to Hour

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

Hour is used as unit of time and Week is a combination of 7 days starting from Monday to Sunday.

1 Hour = 0.00595238 Week
1 Week =  168 Hour

Formula to convert Week to Hour.

Hour = Week * 168

Formula to convert Hour to Week.

Week = Hour / 168

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

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

Approach:

  • Declare Hour and Week value.
  • Then convert Hour to Week and Week 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);
        //hour value declared
        double hour = 450;
        //week value declared
        double week = 3.5;

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

Value of 450.0 hour in week: 2.6785714285714284
Value of 3.5 week in hour: 588.0

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

Approach:

  • Take user input of Hour and Week value.
  • Then convert Hour to Week and Week 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 week
        System.out.println("Enter value of week: ");  
        double week = sc.nextDouble();
        
        //converting hour to week
        double wk = hour / 168;
        //converting week to hour 
        double hr = week * 168;
        //printing result
        System.out.println("Value of "+hour+" hour in week: "+ wk);   
        System.out.println("Value of "+week+" week in hour: "+ hr);   
   }
}
Output:

Enter value of hour: 
300
Enter value of week: 
4
Value of 300.0 hour in week: 1.7857142857142858
Value of 4.0 week in hour: 672.0

Method-3: Java Program to Convert Hour to Week and Week 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 Week value as parameter.
  • Inside method convert Hour to Week and Week 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 week
        System.out.println("Enter value of week: ");  
        double week = sc.nextDouble();
         //calling user defined method convert()
        convert(hour, week);
   }
   
   //convert() method to convert day to hour and vice versa
   public static void convert(double hour, double week)
   {
        //converting hour to week
        double wk = hour / 168;
        //converting week to hour 
        double hr = week * 168;
        //printing result
        System.out.println("Value of "+hour+" hour in week: "+ wk);   
        System.out.println("Value of "+week+" week in hour: "+ hr);   
   }
}
Output:

Enter value of hour: 
300
Enter value of week: 
4
Value of 300.0 hour in week: 1.7857142857142858
Value of 4.0 week in hour: 672.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: