Java LocalDateTime getDayOfYear() Method

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

Definition

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

ParameterDescription
NonegetDayOfYear() takes no parameters.

Return Value

Return ValueDescription
intThe day of the year, from 1 to 365, or to 366 in a leap year.

Compare with Similar Methods

Other ways to read part of a date-time:

MethodHow it differs
getDayOfMonth()Reads the day number of the month.
getDayOfWeek()Reads the weekday.
withDayOfYear(d)Sets the day number instead of reading it.

Java Examples

Example 1 — Read the Day of the Year

Read the day number of the first day of the year, 15 March, and the last day.

Java
import java.time.LocalDateTime;

public class LdtGetDayOfYearDemo {
    public static void main(String[] args) {
        LocalDateTime newYear = LocalDateTime.of(2026, 1, 1, 0, 0);
        LocalDateTime march = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime lastDay = LocalDateTime.of(2026, 12, 31, 23, 59);

        System.out.println("1 Jan:  " + newYear.getDayOfYear());
        System.out.println("15 Mar: " + march.getDayOfYear());
        System.out.println("31 Dec: " + lastDay.getDayOfYear());
    }
}

How It Works

  1. 1 January is day 1. The count starts at 1, not 0.
  2. 15 March. January has 31 days and February has 28, so 31 + 28 + 15 is day 74.
  3. 31 December. 2026 is not a leap year, so the last day is 365.

Example 2 — Compare a Normal and a Leap Year

Read the day number of the same dates in 2026 and in the leap year 2024.

Java
import java.time.LocalDateTime;

public class LdtGetDayOfYearLeap {
    public static void main(String[] args) {
        LocalDateTime[] normal = {
            LocalDateTime.of(2026, 3, 15, 8, 0),
            LocalDateTime.of(2026, 12, 31, 8, 0)
        };
        LocalDateTime[] leap = {
            LocalDateTime.of(2024, 3, 15, 8, 0),
            LocalDateTime.of(2024, 12, 31, 8, 0)
        };

        System.out.println("2026: " + normal[0].getDayOfYear() + " and " + normal[1].getDayOfYear());
        System.out.println("2024: " + leap[0].getDayOfYear() + " and " + leap[1].getDayOfYear());
    }
}

How It Works

  1. Same dates, different years. The arrays hold 15 March and 31 December of 2026 and 2024.
  2. 2026 is a normal year. The numbers are 74 and 365.
  3. 2024 is a leap year. February has 29 days, so the numbers are one higher: 75 and 366.

Example 3 — Count the Days Left in the Year

Use the length of the year and the day number to find how many days are left.

Java
import java.time.LocalDateTime;

public class LdtGetDayOfYearLeft {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 11, 19, 9, 0);

        int dayNumber = dt.getDayOfYear();
        int yearLength = dt.toLocalDate().lengthOfYear();

        System.out.println("day number:  " + dayNumber);
        System.out.println("year length: " + yearLength);
        System.out.println("days left:   " + (yearLength - dayNumber));
    }
}

How It Works

  1. 19 November 2026. It is a late date in a normal year.
  2. The day number. getDayOfYear() returns 323.
  3. The year length. lengthOfYear() returns 365 for 2026.
  4. Subtract. 365 minus 323 leaves 42 days until the end of the year.

Frequently Asked Questions

Call dt.getDayOfYear(). It returns a number from 1 to 366.
No. It only reads a value. LocalDateTime is immutable, so no method can change it.
Use dt.getDayOfYear(). It is 1 on 1 January.
365 in a normal year and 366 in a leap year.
Use withDayOfYear(number) on a date-time of the right year, for example dt.withDayOfYear(100).

Next: getHour()

Read the hour on a 24-hour clock, from 0 to 23.

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

7 people found this page helpful