Java LocalDateTime getHour() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, getHour() returns the hour of a LocalDateTime on a 24-hour clock. Midnight is 0 and 11 at night is 23.

It does not tell you AM or PM, but you can work that out: hours below 12 are AM and the rest are PM.

An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file.

Syntax

Call it on a LocalDateTime. It takes no arguments.

Java
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 45);
int hour = dt.getHour();   // 14
CallWhat it does
dt.getHour()Returns the hour from 0 to 23, for example 14.
dt.getMinute()Returns the minute of the hour, 0 to 59.
dt.getSecond()Returns the second of the minute, 0 to 59.
dt.get(ChronoField.HOUR_OF_AMPM)Returns the hour on a 12-hour clock, 0 to 11.

There is no hour 24. Midnight is hour 0 of the next day.

Library declarations
Java
// java.time.LocalDateTime
public int getHour()

Parameters

ParameterDescription
NonegetHour() takes no parameters.

Return Value

Return ValueDescription
intThe hour of the day, from 0 to 23.

Compare with Similar Methods

Other ways to read part of a date-time:

MethodHow it differs
getMinute()Reads the minute.
getSecond()Reads the second.
withHour(h)Sets the hour instead of reading it.

Java Examples

Example 1 — Read the Hour of the Day

Read the hour of an afternoon time and of midnight.

Java
import java.time.LocalDateTime;

public class LdtGetHourDemo {
    public static void main(String[] args) {
        LocalDateTime afternoon = LocalDateTime.of(2026, 3, 15, 14, 45);
        LocalDateTime midnight = LocalDateTime.of(2026, 3, 15, 0, 5);

        System.out.println("afternoon: " + afternoon.getHour());
        System.out.println("midnight:  " + midnight.getHour());
    }
}

How It Works

  1. Two times. 14:45 and 00:05.
  2. A 24-hour clock. The afternoon time gives 14, not 2.
  3. Midnight is 0. The hour just after midnight is 0.

Example 2 — Choose a Greeting by Hour

Show a different greeting for the morning, the afternoon, and the evening.

Java
import java.time.LocalDateTime;

public class LdtGetHourGreeting {
    public static void main(String[] args) {
        int[] hours = {6, 13, 20};

        for (int h : hours) {
            LocalDateTime dt = LocalDateTime.of(2026, 3, 15, h, 0);
            String greeting;
            if (dt.getHour() < 12) {
                greeting = "Good morning";
            } else if (dt.getHour() < 18) {
                greeting = "Good afternoon";
            } else {
                greeting = "Good evening";
            }
            System.out.println(dt.getHour() + ":00 -> " + greeting);
        }
    }
}

How It Works

  1. Three hours. The loop builds a date-time for 6, 13, and 20 o'clock.
  2. Before 12 is morning. Hour 6 gets "Good morning".
  3. 12 to 17 is afternoon. Hour 13 gets "Good afternoon".
  4. The rest is evening. Hour 20 gets "Good evening".

Example 3 — Show a 12-Hour Clock

Convert the 24-hour value to a 12-hour value with AM or PM for midnight, noon, and 3 in the afternoon.

Java
import java.time.LocalDateTime;

public class LdtGetHourTwelve {
    public static void main(String[] args) {
        int[] hours = {0, 12, 15};

        for (int h : hours) {
            LocalDateTime dt = LocalDateTime.of(2026, 3, 15, h, 0);
            int hour12 = dt.getHour() % 12 == 0 ? 12 : dt.getHour() % 12;
            String suffix = dt.getHour() < 12 ? "AM" : "PM";
            System.out.println(dt.getHour() + " -> " + hour12 + " " + suffix);
        }
    }
}

How It Works

  1. Remainder of 12. getHour() % 12 maps 13 to 1, 15 to 3, and so on.
  2. Zero becomes 12. Both midnight (0) and noon (12) give a remainder of 0, which is shown as 12 on a clock.
  3. AM or PM. Hours below 12 are AM, and the rest are PM.
  4. The results. 0 gives 12 AM, 12 gives 12 PM, and 15 gives 3 PM.

Frequently Asked Questions

Call dt.getHour(). It returns a number from 0 to 23.
No. It only reads a value. LocalDateTime is immutable, so no method can change it.
Use LocalDateTime.now().getHour(). It is a number from 0 to 23.
The 24-hour value. For 2:45 PM it returns 14.
Use format() with a pattern such as DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH).

Next: getLong()

Read any field of the date-time as a long.

getLong() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful