Java LocalDateTime withDayOfYear() Method

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

Definition

In Java, withDayOfYear(dayOfYear) returns a new LocalDateTime with the day of the year set to the value you pass. The year and the time of day are copied from the original. The month and the day are worked out from the number.

LocalDateTime is immutable, so the original object never changes. Always keep the result: dt = dt.withDayOfYear(100);. Day 1 is 1 January and day 365 is 31 December in a normal 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 and pass the new day of the year.

Java
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.withDayOfYear(100);   // 2026-04-10T10:30
System.out.println(start);   // 2026-03-15T10:30
CallWhat it does
dt.withDayOfYear(n)Returns a copy with the day of the year replaced by n.
dt.with(ChronoField.DAY_OF_YEAR, n)The general form for any field. It does the same thing here.
dt.getDayOfYear()Reads the day number, the opposite of setting it.
date.withDayOfYear(d)LocalDate has the same method, but it has no time of day.

The result is a new object. Everything except the day of the year is copied from the original.

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

Parameters

ParameterDescription
dayOfYear (int)The new day of the year, from 1 to 365, or to 366 in a leap year.

Return Value

Return ValueDescription
LocalDateTimeA new LocalDateTime with the day of the year replaced. The original is not changed.

Compare with Similar Methods

Ways to change one part of a date-time:

MethodHow it differs
with(field, value)The general method: you choose the field as an argument.
getDayOfYear()Reads the day number of the year.
withDayOfMonth(d)Sets the day inside the current month.

Java Examples

Example 1 — Turn Day Numbers into Dates

Set the day of the year to 1, 100, and 365.

Java
import java.time.LocalDateTime;

public class LdtWithDayOfYearDemo {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);

        System.out.println("day 1:   " + start.withDayOfYear(1));
        System.out.println("day 100: " + start.withDayOfYear(100));
        System.out.println("day 365: " + start.withDayOfYear(365));
    }
}

How It Works

  1. Day 1. It is 1 January 2026, and the time stays 10:30.
  2. Day 100. January, February, and March use 90 days, so day 100 is 10 April.
  3. Day 365. The last day of a normal year is 31 December.

Example 2 — Try Day 366 in Normal and Leap Years

Set day 366 in 2026 (a normal year) and in 2024 (a leap year).

Java
import java.time.DateTimeException;
import java.time.LocalDateTime;

public class LdtWithDayOfYearLeap {
    public static void main(String[] args) {
        LocalDateTime normal = LocalDateTime.of(2026, 3, 15, 10, 30);
        LocalDateTime leap = LocalDateTime.of(2024, 3, 15, 10, 30);

        try {
            normal.withDayOfYear(366);
        } catch (DateTimeException e) {
            System.out.println("2026, day 366 -> DateTimeException");
        }
        System.out.println("2024, day 366 -> " + leap.withDayOfYear(366));
    }
}

How It Works

  1. 2026 has 365 days. There is no day 366, so the call throws a DateTimeException.
  2. 2024 has 366 days. It is a leap year, so day 366 exists.
  3. It is 31 December. The last day of a leap year is 31 December 2024, at the same 10:30.

Example 3 — Find Programmers' Day

Day 256 is Programmers' Day. Find its date in a normal year and in a leap year.

Java
import java.time.LocalDateTime;

public class LdtWithDayOfYearProgrammer {
    public static void main(String[] args) {
        LocalDateTime normal = LocalDateTime.of(2026, 1, 1, 0, 0);
        LocalDateTime leap = LocalDateTime.of(2028, 1, 1, 0, 0);

        System.out.println("2026: " + normal.withDayOfYear(256));
        System.out.println("2028: " + leap.withDayOfYear(256));
    }
}

How It Works

  1. Both start on 1 January. Only the year of each object matters for the number.
  2. 2026 is a normal year. Day 256 is 13 September.
  3. 2028 is a leap year. February has an extra day, so the same number falls one day earlier, on 12 September.

Frequently Asked Questions

Call dt.withDayOfYear(value). It returns a new LocalDateTime with that part replaced and everything else copied.
No. LocalDateTime is immutable, so withDayOfYear() returns a new object and leaves the original as it was.
Day 366 in a normal year, and any number below 1 or above 366, throws a DateTimeException.
Create a date-time in that year and call withDayOfYear(number), for example LocalDateTime.of(2026, 1, 1, 0, 0).withDayOfYear(100).
In a normal year it is 10 April, and in a leap year it is 9 April.

Next: withHour()

Return a copy with a different hour.

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

8 people found this page helpful