Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

TimeUnit toSeconds() method in Java with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The toSeconds() 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 toSeconds(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 program illustrate the implementation of TimeUnit toSeconds() method: Program 1: 

Java




// Java program to demonstrate
// toSeconds() 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 Seconds
        // using toSeconds() method
        System.out.println("Time " + timeInMilliSec
                           + " milliSeconds in Seconds = "
                           + time.toSeconds(timeInMilliSec));
    }
}


Output:

Time 1539585656193 milliSeconds in Seconds = 1539585656

Program 2: 

Java




// Java program to demonstrate
// toSeconds() 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 Seconds
        // using toSeconds() method
        System.out.println("Time " + timeInMilliSec
                           + " milliSeconds in Seconds = "
                           + time.toSeconds(timeInMilliSec));
    }
}


Output:

Time 1539585659342 milliSeconds in Seconds = 1539585659

My Personal Notes arrow_drop_up
Last Updated : 17 Nov, 2022
Like Article
Save Article
Similar Reads
Related Tutorials