Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

TimeUnit toMillis() method in Java with Examples

  • Last Updated : 30 Oct, 2018

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

Syntax:

public long toMillis(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 Seconds.

Below programt illustrate the implementation of TimeUnit toMillis() method:



Program 1:




// Java program to demonstrate
// toMillis() method of TimeUnit Class
  
import java.util.concurrent.*;
import java.util.Date;
  
class GFG {
    public static void main(String args[])
    {
        // Get current time in seconds
        long timeInSec = (new Date().getTime()) / 1000;
  
        // Create a TimeUnit object
        TimeUnit time = TimeUnit.SECONDS;
  
        // Convert seconds to milliSeconds
        // using toMillis() method
        System.out.println("Time " + timeInSec
                           + " Seconds in MilliSeconds = "
                           + time.toMillis(timeInSec));
    }
}
Output:
Time 1539585757 Seconds in MilliSeconds = 1539585757000

Program 2:




// Java program to demonstrate
// toMillis() 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 timeInSec = (Calendar
                              .getInstance()
                              .getTimeInMillis())
                         / 1000;
  
        // Create a TimeUnit object
        TimeUnit time = TimeUnit.SECONDS;
  
        // Convert seconds to milliSeconds
        // using toMillis() method
        System.out.println("Time " + timeInSec
                           + " Seconds in MilliSeconds = "
                           + time.toMillis(timeInSec));
    }
}
Output:
Time 1539585759 Seconds in MilliSeconds = 1539585759000




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!