In Java, getDayOfYear() returns the position of a date inside its year, counting from 1 on 1 January. It is 365 on 31 December in a normal year and 366 in a leap year.
This number is sometimes called the "ordinal date". It is handy for progress bars, deadlines, and simple date differences inside one year.
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.getDayOfYear(); // 74 (31 + 28 + 15)
Call
What it does
dt.getDayOfYear()
Returns the day number inside the year, for example 74.
dt.getDayOfMonth()
Returns the number on the calendar page, 1 to 31.
dt.toLocalDate().lengthOfYear()
Returns 365 or 366, the number of days in the year.
dt.get(ChronoField.DAY_OF_YEAR)
The general form: it gives the same number.
The same calendar date can have a different day number in a leap year, because of 29 February.
Library declarations
Java
// java.time.LocalDateTime
public int getDayOfYear()
Parameters
Parameter
Description
None
getDayOfYear() takes no parameters.
Return Value
Return Value
Description
int
The day of the year, from 1 to 365, or to 366 in a leap year.