Home Java LocalDateTime Methods withDayOfYear() Java LocalDateTime withDayOfYear() Method 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.
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 Call What 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.time.LocalDateTime
public LocalDateTime withDayOfYear(int dayOfYear) Parameters Parameter Description dayOfYear (int)The new day of the year, from 1 to 365, or to 366 in a leap year.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the day of the year replaced. The original is not changed.
Important
Day 366 only exists in a leap year. In a normal year, withDayOfYear(366) throws a DateTimeException. The number counts from 1, and it is based on the year of the date, so the same number gives a different month and day in leap years after February.
Compare with Similar Methods Ways to change one part of a date-time:
Java Examples 1. Pick a day by number 2. Leap year 3. Day 256 Example 1 — Turn Day Numbers into Dates Set the day of the year to 1, 100, and 365.
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));
}
} day 1: 2026-01-01T10:30
day 100: 2026-04-10T10:30
day 365: 2026-12-31T10:30 How It Works Day 1. It is 1 January 2026, and the time stays 10:30.Day 100. January, February, and March use 90 days, so day 100 is 10 April.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).
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));
}
} 2026, day 366 -> DateTimeException
2024, day 366 -> 2024-12-31T10:30 How It Works 2026 has 365 days. There is no day 366, so the call throws a DateTimeException.2024 has 366 days. It is a leap year, so day 366 exists.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.
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));
}
} 2026: 2026-09-13T00:00
2028: 2028-09-12T00:00 How It Works Both start on 1 January. Only the year of each object matters for the number.2026 is a normal year. Day 256 is 13 September.2028 is a leap year. February has an extra day, so the same number falls one day earlier, on 12 September.Frequently Asked Questions How do I change only the day of the year of a LocalDateTime in Java? Call dt.withDayOfYear(value). It returns a new LocalDateTime with that part replaced and everything else copied.
Does withDayOfYear() change the original LocalDateTime? No. LocalDateTime is immutable, so withDayOfYear() returns a new object and leaves the original as it was.
What happens if the value passed to withDayOfYear() is not valid? Day 366 in a normal year, and any number below 1 or above 366, throws a DateTimeException.
How do I convert a day number to a date in Java? Create a date-time in that year and call withDayOfYear(number), for example LocalDateTime.of(2026, 1, 1, 0, 0).withDayOfYear(100).
What is the 100th day of the year in Java? In a normal year it is 10 April, and in a leap year it is 9 April.
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.
8 people found this page helpful
Helpful Share Copy link Suggestion