Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaTimeUnit toMicros() method in Java with Examples

TimeUnit toMicros() method in Java with Examples

The toMicros() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of MicroSeconds, since midnight UTC on the 1st January 1970.

Syntax:  

public long toMicros(long duration)

Parameters: This method accepts a mandatory parameter duration which is the duration in milliSeconds.

Return Value: This method returns the converted duration as MicroSeconds.

Below program illustrate the implementation of TimeUnit toMicros() method:

Program 1: 

Java




// Java program to demonstrate
// toMicros() method of TimeUnit Class
 
import java.util.concurrent.*;
import java.util.Date;
 
class GFG {
    public static void main(String args[])
    {
        // Get current time in milliseconds
        long timeInMilliSec = new Date().getTime();
 
        // Create a TimeUnit object
        TimeUnit time = TimeUnit.MILLISECONDS;
 
        // Convert milliseconds to MicroSeconds
        // using toMicros() method
        System.out.println("Time " + timeInMilliSec
                           + " milliSeconds in MicroSeconds = "
                           + time.toMicros(timeInMilliSec));
    }
}


Output: 

Time 1539585538596 milliSeconds in MicroSeconds = 1539585538596000

 

Program 2:

Java




// Java program to demonstrate
// toMicros() method of TimeUnit Class
 
import java.util.concurrent.*;
import java.util.Calendar;
 
class GFG {
    public static void main(String args[])
    {
        // Get current time in milliseconds
        long timeInMilliSec = Calendar
                                  .getInstance()
                                  .getTimeInMillis();
 
        // Create a TimeUnit object
        TimeUnit time = TimeUnit.MILLISECONDS;
 
        // Convert milliseconds to MicroSeconds
        // using toMicros() method
        System.out.println("Time " + timeInMilliSec
                           + " milliSeconds in MicroSeconds = "
                           + time.toMicros(timeInMilliSec));
    }
}


Output: 

Time 1539585540908 milliSeconds in MicroSeconds = 1539585540908000

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments