Home Java LocalDateTime Methods getHour() Java LocalDateTime getHour() Method 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.
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 45);
int hour = dt.getHour(); // 14 Call What 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.time.LocalDateTime
public int getHour() Parameters Parameter Description None getHour() takes no parameters.
Return Value Return Value Description intThe hour of the day, from 0 to 23.
Compare with Similar Methods Other ways to read part of a date-time:
Java Examples 1. Read the hour 2. Greeting 3. 12-hour clock Example 1 — Read the Hour of the Day Read the hour of an afternoon time and of midnight.
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());
}
} afternoon: 14
midnight: 0 How It Works Two times. 14:45 and 00:05.A 24-hour clock. The afternoon time gives 14, not 2.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.
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);
}
}
} 6:00 -> Good morning
13:00 -> Good afternoon
20:00 -> Good evening How It Works Three hours. The loop builds a date-time for 6, 13, and 20 o'clock.Before 12 is morning. Hour 6 gets "Good morning".12 to 17 is afternoon. Hour 13 gets "Good afternoon".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.
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);
}
}
} 0 -> 12 AM
12 -> 12 PM
15 -> 3 PM How It Works Remainder of 12. getHour() % 12 maps 13 to 1, 15 to 3, and so on.Zero becomes 12. Both midnight (0) and noon (12) give a remainder of 0, which is shown as 12 on a clock.AM or PM. Hours below 12 are AM, and the rest are PM.The results. 0 gives 12 AM, 12 gives 12 PM, and 15 gives 3 PM.Frequently Asked Questions How do I get the hour from a LocalDateTime in Java? Call dt.getHour(). It returns a number from 0 to 23.
Does getHour() change the LocalDateTime? No. It only reads a value. LocalDateTime is immutable, so no method can change it.
How do I get the current hour in Java? Use LocalDateTime.now().getHour(). It is a number from 0 to 23.
Does getHour() return the 12-hour or 24-hour value? The 24-hour value. For 2:45 PM it returns 14.
How do I show the time with AM and PM? Use format() with a pattern such as DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion