Friday, October 10, 2025
HomeLanguagesJavaTimeUnit toDays() method in Java with Examples

TimeUnit toDays() method in Java with Examples

The toDays() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Days, since midnight UTC on the 1st January 1970. It is equivalent to DAYS.convert(duration, this). Syntax:

public long toDays(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 Days. Below program to illustrate the implementation of TimeUnit toDays() method: Program 1: 

Java




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


Output:

Time 1539585725051 milliSeconds in Days = 17819

Program 2: 

Java




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


Output:

Time 1539585730178 milliSeconds in Days = 17819
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS