Thursday, May 7, 2026
HomeLanguagesJavaLocalTime getHour() method in Java with Examples

LocalTime getHour() method in Java with Examples

The getHour() method of a LocalTime class is used to return the hour-of-day field. This method returns an integer value ranging from 0 to 23, i.e. the Hours of a day.

Syntax:

public int getHour()

Parameters: This method does not accept any parameter.

Return value: This method returns an integer value which represents hour-of-day and it ranges from 0 to 23.

Below programs illustrate the getHour() method:

Program 1:




// Java program to demonstrate
// LocalTime.getHour() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("19:34:50.63");
  
        // get Hour field using getHour()
        int Hour = time.getHour();
  
        // print result
        System.out.println("Hour Field: "
                           + Hour);
    }
}


Output:

Hour Field: 19

Program 2:




// Java program to demonstrate
// LocalTime.getHour() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("23:14:30.53");
  
        // get Hour field using getHour()
        int Hour = time.getHour();
  
        // print result
        System.out.println("Hour Field: "
                           + Hour);
    }
}


Output:

Hour Field: 23

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#getHour()

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6891 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12105 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6962 POSTS0 COMMENTS