Java LocalDateTime getMonthValue() Method

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

Definition

In Java, getMonthValue() returns the month of a LocalDateTime as an int. January is 1 and December is 12.

This is different from the old Calendar class, where January was 0. If you want the name instead of the number, use getMonth().

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 month = dt.getMonthValue();   // 3
Month name = dt.getMonth();       // MARCH
CallWhat it does
dt.getMonthValue()Returns the month number, for example 3 for March.
dt.getMonth()Returns the month as a Month enum, for example MARCH.
dt.getYear()Returns the year.
dt.get(ChronoField.MONTH_OF_YEAR)The general form: it gives the same number.

The number is the same one that withMonth() and LocalDateTime.of() expect.

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

Parameters

ParameterDescription
NonegetMonthValue() takes no parameters.

Return Value

Return ValueDescription
intThe month, from 1 (January) to 12 (December).

Compare with Similar Methods

Other ways to read part of a date-time:

MethodHow it differs
getMonth()Returns the same month as an enum with a name.
withMonth(m)Sets the month instead of reading it.
getYear()Reads the year.

Java Examples

Example 1 — Read the Month Number

Read the month number of a March date and a December date.

Java
import java.time.LocalDateTime;

public class LdtGetMonthValueDemo {
    public static void main(String[] args) {
        LocalDateTime spring = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime winter = LocalDateTime.of(2026, 12, 25, 8, 0);

        System.out.println("March is month    " + spring.getMonthValue());
        System.out.println("December is month " + winter.getMonthValue());
    }
}

How It Works

  1. Two dates. One in March and one in December.
  2. March is 3. The month numbers start at 1 for January.
  3. December is 12. There is no month 0 or month 13.

Example 2 — Work Out the Quarter

Turn a month number into a quarter from 1 to 4 for several dates.

Java
import java.time.LocalDateTime;

public class LdtGetMonthValueQuarter {
    public static void main(String[] args) {
        LocalDateTime[] dates = {
            LocalDateTime.of(2026, 1, 10, 9, 0),
            LocalDateTime.of(2026, 5, 5, 9, 0),
            LocalDateTime.of(2026, 8, 20, 9, 0),
            LocalDateTime.of(2026, 12, 1, 9, 0)
        };

        for (LocalDateTime dt : dates) {
            int quarter = (dt.getMonthValue() - 1) / 3 + 1;
            System.out.println("month " + dt.getMonthValue() + " -> Q" + quarter);
        }
    }
}

How It Works

  1. Four dates. They are in January, May, August, and December.
  2. Subtract 1 first. getMonthValue() - 1 turns the months into 0 to 11.
  3. Divide by 3. Integer division groups three months together, and adding 1 makes the quarters 1 to 4.
  4. The results. Months 1, 5, 8, and 12 give Q1, Q2, Q3, and Q4.

Example 3 — Build a File Name with a Padded Month

Put the year and the month in a file name, with the month padded to two digits.

Java
import java.time.LocalDateTime;

public class LdtGetMonthValuePad {
    public static void main(String[] args) {
        LocalDateTime march = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime november = LocalDateTime.of(2026, 11, 19, 16, 45);

        System.out.println(String.format("report-%d-%02d.txt", march.getYear(), march.getMonthValue()));
        System.out.println(String.format("report-%d-%02d.txt", november.getYear(), november.getMonthValue()));
    }
}

How It Works

  1. Two numbers. The year and the month are both plain int values.
  2. %02d pads with a zero. The month 3 becomes 03, so the names sort correctly.
  3. Two-digit months stay as they are. November is 11, so nothing is added.

Frequently Asked Questions

Call dt.getMonthValue(). It returns a number from 1 to 12.
No. It only reads a value. LocalDateTime is immutable, so no method can change it.
1. getMonthValue() returns 1 for January and 12 for December.
getMonth() returns a Month enum such as MARCH. getMonthValue() returns the number 3.
Use dt.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH), or format the date with the pattern MMMM.

Next: getNano()

Read the fraction of the second, in nanoseconds.

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

12 people found this page helpful