In the previous article, we have discussed about Java Program to Convert Hour to Second and Second to Hour
In this article we will see how to convert Hour to Millisecond and Millisecond to Hour by using Java programming language.
Java Program to Convert Hour to Millisecond and Millisecond to Hour
Before jumping into the program let’s know the relationship between Hour and Millisecond and how we can convert Hour to Millisecond and vice versa.
Hour and Millisecond are used as unit of time.
1 Hour = 3.6e+6 Millisecond 1 Millisecond = 2.7778e-7 Hour
Formula to convert Millisecond to Hour.
Hour = Millisecond / 3.6e+6
Formula to convert Hour to Millisecond.
Millisecond= Hour * 3.6e+6
Let’s see different ways to convert Hour to Millisecond and Millisecond to Hour.
Method-1: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using Static Input Value
Approach:
- Declare Hour and Millisecond value.
- Then convert Hour to Millisecond and Millisecond 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 millisecond declared double millisecond = 1; //converting hour to millisecond double ms = hour * 3.6e+6; //converting millisecond to hour double hr = millisecond / 3.6e+6; //printing result System.out.println("Value of "+hour+" hour in millisecond: "+ ms); System.out.println("Value of "+millisecond+" millisecond in hour: "+ hr); } }
Output: Value of 1.0 hour in millisecond: 3600000.0 Value of 1.0 millisecond in hour: 2.7777777777777776E-7
Method-2: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using User Input Value
Approach:
- Take user input of Hour and Millisecond value.
- Then convert Hour to Millisecond and Millisecond 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 millisecond System.out.println("Enter value of millisecond: "); double millisecond = sc.nextDouble(); //converting hour to millisecond double ms = hour * 3.6e+6; //converting millisecond to hour double hr = millisecond / 3.6e+6; //printing result System.out.println("Value of "+hour+" hour in millisecond: "+ ms); System.out.println("Value of "+millisecond+" millisecond in hour: "+ hr); } }
Output: Enter value of hour: 3 Enter value of millisecond: 900000 Value of 3.0 hour in millisecond: 1.08E7 Value of 900000.0 millisecond in hour: 0.25
Method-3: Java Program to Convert Hour to Millisecond and Millisecond to Hour By Using User Defined Method
Approach:
- Take user input of Hour and Millisecond value.
- Call a user defined method by passing Hour and Millisecond value as parameter.
- Inside method convert Hour to Millisecond and Millisecond 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 millisecond System.out.println("Enter value of millisecond: "); double millisecond = sc.nextDouble(); //calling user defined method convert() convert(hour, millisecond); } //convert() method to convert second to hour and vice versa public static void convert(double hour, double millisecond) { //converting hour to millisecond double ms = hour * 3.6e+6; //converting millisecond to hour double hr = millisecond / 3.6e+6; //printing result System.out.println("Value of "+hour+" hour in millisecond: "+ ms); System.out.println("Value of "+millisecond+" millisecond in hour: "+ hr); } }
Output: Enter value of hour: 5 Enter value of millisecond: 1000000 Value of 5.0 hour in millisecond: 1.8E7 Value of 1000000.0 millisecond in hour: 0.2777777777777778
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: