Home Java LocalDateTime Methods getMonthValue() Java LocalDateTime getMonthValue() Method 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.
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);
int month = dt.getMonthValue(); // 3
Month name = dt.getMonth(); // MARCH Call What 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.time.LocalDateTime
public int getMonthValue() Parameters Parameter Description None getMonthValue() takes no parameters.
Return Value Return Value Description intThe month, from 1 (January) to 12 (December).
Compare with Similar Methods Other ways to read part of a date-time:
Java Examples 1. Read the month 2. Quarter of the year 3. Zero-padded name Example 1 — Read the Month Number Read the month number of a March date and a December date.
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());
}
} March is month 3
December is month 12 How It Works Two dates. One in March and one in December.March is 3. The month numbers start at 1 for January.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.
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);
}
}
} month 1 -> Q1
month 5 -> Q2
month 8 -> Q3
month 12 -> Q4 How It Works Four dates. They are in January, May, August, and December.Subtract 1 first. getMonthValue() - 1 turns the months into 0 to 11.Divide by 3. Integer division groups three months together, and adding 1 makes the quarters 1 to 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.
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()));
}
} report-2026-03.txt
report-2026-11.txt How It Works Two numbers. The year and the month are both plain int values.%02d pads with a zero. The month 3 becomes 03, so the names sort correctly.Two-digit months stay as they are. November is 11, so nothing is added.Frequently Asked Questions How do I get the month number from a LocalDateTime in Java? Call dt.getMonthValue(). It returns a number from 1 to 12.
Does getMonthValue() change the LocalDateTime? No. It only reads a value. LocalDateTime is immutable, so no method can change it.
Is January 0 or 1 in java.time? 1. getMonthValue() returns 1 for January and 12 for December.
What is the difference between getMonth() and getMonthValue()? getMonth() returns a Month enum such as MARCH. getMonthValue() returns the number 3.
How do I get the month as text like "March"? Use dt.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH), or format the date with the pattern MMMM.
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.
12 people found this page helpful
Helpful Share Copy link Suggestion