Java LocalDateTime getDayOfMonth() Method

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

Definition

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
CallWhat 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

ParameterDescription
NonegetDayOfMonth() takes no parameters.

Return Value

Return ValueDescription
intThe day of the month, from 1 to 31 (28 to 31 depending on the month).

Compare with Similar Methods

Other ways to read part of a date-time:

MethodHow it differs
getDayOfWeek()Reads the weekday, such as MONDAY.
getDayOfYear()Reads the day number inside the year.
withDayOfMonth(d)Sets the day instead of reading it.

Java Examples

Example 1 — Read the Day of 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());
    }
}

How It Works

  1. Three dates in March. The first, the middle, and the last day.
  2. The day number only. getDayOfMonth() ignores the year, month, and time.
  3. 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()));
    }
}

How It Works

  1. The same day number. Both dates are on day 28 of February.
  2. The month length differs. lengthOfMonth() is 28 in 2026 and 29 in the leap year 2024.
  3. 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);
        }
    }
}

How It Works

  1. Three dates. The 1st, the 15th, and the 20th of March.
  2. Read the day. getDayOfMonth() gives 1, 15, and 20.
  3. 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.

Next: getDayOfWeek()

Read the weekday as a DayOfWeek enum such as SUNDAY.

getDayOfWeek() 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.

14 people found this page helpful