In Java, getDayOfMonth() returns the day of the month of a LocalDateTime as an int, from 1 up to the length of the month (28, 29, 30, or 31).
It is the number you see on a calendar. It is not the day of the week (use getDayOfWeek()) and not the day of the year (use getDayOfYear()).
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, 10, 30);
int day = dt.getDayOfMonth(); // 15
Call
What it does
dt.getDayOfMonth()
Returns the day of the month, for example 15.
dt.getDayOfWeek()
Returns the weekday as an enum, such as SUNDAY.
dt.getDayOfYear()
Returns the day number inside the year, 1 to 366.
dt.get(ChronoField.DAY_OF_MONTH)
The general form: it gives the same number.
The number is the same one that withDayOfMonth() and LocalDateTime.of() expect.
Library declarations
Java
// java.time.LocalDateTime
public int getDayOfMonth()
Parameters
Parameter
Description
None
getDayOfMonth() takes no parameters.
Return Value
Return Value
Description
int
The day of the month, from 1 to 31 (28 to 31 depending on the month).
Read the day number of the first, the middle, and the last day of March.
Java
import java.time.LocalDateTime;
public class LdtGetDayOfMonthDemo {
public static void main(String[] args) {
LocalDateTime first = LocalDateTime.of(2026, 3, 1, 8, 0);
LocalDateTime middle = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime last = LocalDateTime.of(2026, 3, 31, 23, 59);
System.out.println("first: " + first.getDayOfMonth());
System.out.println("middle: " + middle.getDayOfMonth());
System.out.println("last: " + last.getDayOfMonth());
}
}
Output
first: 1
middle: 15
last: 31
How It Works
Three dates in March. The first, the middle, and the last day.
The day number only.getDayOfMonth() ignores the year, month, and time.
The results. The program prints 1, 15, and 31.
Example 2 — Check for the Last Day of the Month
Compare the day with the length of its month, in a normal year and in a leap year.
Java
import java.time.LocalDateTime;
public class LdtGetDayOfMonthLast {
public static void main(String[] args) {
LocalDateTime feb2026 = LocalDateTime.of(2026, 2, 28, 12, 0);
LocalDateTime feb2024 = LocalDateTime.of(2024, 2, 28, 12, 0);
System.out.println("28 Feb 2026 is the last day: "
+ (feb2026.getDayOfMonth() == feb2026.toLocalDate().lengthOfMonth()));
System.out.println("28 Feb 2024 is the last day: "
+ (feb2024.getDayOfMonth() == feb2024.toLocalDate().lengthOfMonth()));
}
}
Output
28 Feb 2026 is the last day: true
28 Feb 2024 is the last day: false
How It Works
The same day number. Both dates are on day 28 of February.
The month length differs.lengthOfMonth() is 28 in 2026 and 29 in the leap year 2024.
Compare the two numbers. In 2026 they match, so the answer is true. In 2024 the month has one more day, so the answer is false.
Example 3 — Check for Payday
Pay is given on the 1st and on the 15th. Test three dates.
Java
import java.time.LocalDateTime;
public class LdtGetDayOfMonthPayday {
public static void main(String[] args) {
LocalDateTime[] dates = {
LocalDateTime.of(2026, 3, 1, 9, 0),
LocalDateTime.of(2026, 3, 15, 9, 0),
LocalDateTime.of(2026, 3, 20, 9, 0)
};
for (LocalDateTime dt : dates) {
int day = dt.getDayOfMonth();
boolean payday = day == 1 || day == 15;
System.out.println("day " + day + " is payday: " + payday);
}
}
}
Output
day 1 is payday: true
day 15 is payday: true
day 20 is payday: false
How It Works
Three dates. The 1st, the 15th, and the 20th of March.
Read the day.getDayOfMonth() gives 1, 15, and 20.
Test the day. The condition day == 1 || day == 15 is true for the first two and false for the third.
Frequently Asked Questions
Call dt.getDayOfMonth(). It returns a number from 1 to 31.
No. It only reads a value. LocalDateTime is immutable, so no method can change it.
Use LocalDateTime.now().getDayOfMonth().
getDayOfMonth() is the number on the calendar page, 1 to 31. getDayOfYear() counts from 1 January, so it goes up to 365 or 366.
Use dt.toLocalDate().lengthOfMonth(). It knows about 28, 29, 30, and 31 day months.