Saturday, September 6, 2025
HomeLanguagesJavaTimeUnit toHours() method in Java with Examples

TimeUnit toHours() method in Java with Examples

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

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

Java




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


Output:

Time 1539585595812 milliSeconds in Hours = 427662

Program 2: 

Java




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


Output:

Time 1539585598692 milliSeconds in Hours = 427662
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11804 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6753 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS