Java Program to Convert Second to Millisecond, Microsecond and Vice Versa

In the previous article, we have discussed about Java Program to Convert Fahrenheit to kelvin and Kelvin to Fahrenheit

In this article we will see how to convert second to millisecond and microsecond and vice versa by using Java programming language.

Java Program to Convert Second to Millisecond, Microsecond and Vice Versa

Before jumping into the program let’s know the relationship between second, millisecond and microsecond and how we can convert second to millisecond and microsecond and vice versa.

Second, millisecond and microsecond all are used as unit in case of Time.

1 Second = 1000 Millisecond
1 Second = 1e+6 Microsecond
1 Millisecond = 0.001 Second
1 Microsecond = 1e-6 Second

Formula to convert second to millisecond.

Millisecond = Second * 1000

Formula to convert second to microsecond.

Microsecond = Second * 1e+6

Formula to convert millisecond to second.

Second = Millisecond / 1000

Formula to convert microsecond to second.

Second = Microsecond / 1e+6

Let’s see different ways to convert second to millisecond and microsecond and vice versa.

Method-1: Java Program to Convert Second to Millisecond, Microsecond and Vice Versa By Using Static Input Value

Approach:

  • Declare user input of second, millisecond and microsecond.
  • Then convert second to millisecond, microsecond and vice versa by using the formula.
  • Print result.

Program:

public class Main 
{
   public static void main(String args[])
   {
        //value of second declared
        double second = 1;
        //value of millisecond declared  
        double millisecond = 1;
        //value of microsecond declared  
        double microsecond = 1;
        
        //converting second to millisecond
        double ms = second * 1000;
        //converting second to microsecond
        double us = second * 1e+6;
        //converting millisecond to second
        double s1 = millisecond / 1000;
        //converting microsecond to second
        double s2 = second / 1e+6;
        
        //printing result
        System.out.println("Value of "+second+" second in millisecond: "+ ms);   
        System.out.println("Value of "+second+" second in microsecond: "+ us);   
        System.out.println("Value of "+millisecond+" millisecond in second: "+ s1);   
        System.out.println("Value of "+microsecond+" microsecond in second: "+ s2);  
   }
}
Output:

Value of 1.0 second in millisecond: 1000.0
Value of 1.0 second in microsecond: 1000000.0
Value of 1.0 millisecond in second: 0.001
Value of 1.0 microsecond in second: 1.0E-6

Method-2: Java Program to Convert Second to Millisecond, Microsecond and Vice Versa By Using User Input Value

Approach:

  • Take user input of second, millisecond and microsecond.
  • Then convert second to millisecond, microsecond and vice versa 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 second
        System.out.println("Enter value of second: ");  
        double second = sc.nextDouble();
        //Taking the value input of double variable millisecond
        System.out.println("Enter value of millisecond: ");  
        double millisecond = sc.nextDouble();
        //Taking the value input of double variable microsecond
        System.out.println("Enter value of microsecond: ");  
        double microsecond = sc.nextDouble();
        
        //converting second to millisecond
        double ms = second * 1000;
        //converting second to microsecond
        double us = second * 1e+6;
        //converting millisecond to second
        double s1 = millisecond / 1000;
        //converting microsecond to second
        double s2 = second / 1e+6;
        
        //printing result
        System.out.println("Value of "+second+" second in millisecond: "+ ms);   
        System.out.println("Value of "+second+" second in microsecond: "+ us);   
        System.out.println("Value of "+millisecond+" millisecond in second: "+ s1);   
        System.out.println("Value of "+microsecond+" microsecond in second: "+ s2);  
   }
}
Output:

Enter value of second: 
10
Enter value of millisecond: 
10
Enter value of microsecond: 
10
Value of 10.0 second in millisecond: 10000.0
Value of 10.0 second in microsecond: 1.0E7
Value of 10.0 millisecond in second: 0.01
Value of 10.0 microsecond in second: 1.0E-5

Method-3: Java Program to Convert Second to Millisecond, Microsecond and Vice Versa By Using User Defined Method

Approach:

  • Take user input of second, millisecond and microsecond.
  • Call a user defined method by passing second, millisecond and microsecond.
  • Then convert second to millisecond, microsecond and vice versa 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 second
        System.out.println("Enter value of second: ");  
        double second = sc.nextDouble();
        //Taking the value input of double variable millisecond
        System.out.println("Enter value of millisecond: ");  
        double millisecond = sc.nextDouble();
        //Taking the value input of double variable microsecond
        System.out.println("Enter value of microsecond: ");  
        double microsecond = sc.nextDouble();
        //calling user defined method convert()
        convert(second, millisecond, microsecond);
   }
   
   //convert() method to convert second to millisecond, microsecond and vice-versa
   public static void convert(double second, double millisecond, double microsecond)
   {
        //converting second to millisecond
        double ms = second * 1000;
        //converting second to microsecond
        double us = second * 1e+6;
        //converting millisecond to second
        double s1 = millisecond / 1000;
        //converting microsecond to second
        double s2 = second / 1e+6;
        
        //printing result
        System.out.println("Value of "+second+" second in millisecond: "+ ms);   
        System.out.println("Value of "+second+" second in microsecond: "+ us);   
        System.out.println("Value of "+millisecond+" millisecond in second: "+ s1);   
        System.out.println("Value of "+microsecond+" microsecond in second: "+ s2);  
   }
}
Output:

Enter value of second: 
60
Enter value of millisecond: 
6000
Enter value of microsecond: 
60000
Value of 60.0 second in millisecond: 60000.0
Value of 60.0 second in microsecond: 6.0E7
Value of 6000.0 millisecond in second: 6.0
Value of 60000.0 microsecond in second: 6.0E-5

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Related Java Programs: