Home Java LocalDateTime Methods withYear() Java LocalDateTime withYear() Method Definition In Java, withYear(year) returns a new LocalDateTime with the year set to the value you pass. The month, the day, and the time of day are copied from the original.
LocalDateTime is immutable , so the original object never changes. Always keep the result: dt = dt.withYear(2030);. It is a quick way to move a birthday or a holiday to another 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 year.
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.withYear(2030); // 2030-03-15T10:30
System.out.println(start); // 2026-03-15T10:30 Call What it does dt.withYear(n)Returns a copy with the year replaced by n. dt.with(ChronoField.YEAR, n)The general form for any field. It does the same thing here. dt.plusYears(n)Moves by an amount instead of setting a value. date.withYear(y)LocalDate has the same method, but it has no time of day.
The result is a new object . Everything except the year is copied from the original.
Library declarations // java.time.LocalDateTime
public LocalDateTime withYear(int year) Parameters Parameter Description year (int)The new year, from -999999999 to 999999999.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the year replaced. The original is not changed.
Compare with Similar Methods Ways to change one part of a date-time:
Java Examples 1. Same day, new year 2. Leap day 3. Birthday this year Example 1 — Move a Date to Another Year Take 15 March 2026 at 10:30 and place it in the years 2030 and 1999.
import java.time.LocalDateTime;
public class LdtWithYearDemo {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
System.out.println("start: " + start);
System.out.println("2030: " + start.withYear(2030));
System.out.println("1999: " + start.withYear(1999));
}
} start: 2026-03-15T10:30
2030: 2030-03-15T10:30
1999: 1999-03-15T10:30 How It Works The start. The year is 2026, and the date-time is 15 March at 10:30.Set the year to 2030. withYear(2030) keeps the 15 March and the 10:30.Any year works. Setting 1999 goes back in time, because the method sets a value, it does not add one.Example 2 — See What Happens to 29 February Set the year of a leap day to a normal year and to another leap year.
import java.time.LocalDateTime;
public class LdtWithYearLeap {
public static void main(String[] args) {
LocalDateTime leapDay = LocalDateTime.of(2024, 2, 29, 12, 0);
System.out.println("2025: " + leapDay.withYear(2025));
System.out.println("2028: " + leapDay.withYear(2028));
}
} 2025: 2025-02-28T12:00
2028: 2028-02-29T12:00 How It Works 29 February 2024. This date exists only in leap years.2025 is not a leap year. There is no 29 February, so Java picks 28 February 2025.2028 is a leap year. The date exists, so it stays 29 February.Example 3 — Find the Next Birthday Move a birth date to the current year, check if it has passed, and then use the next year.
import java.time.LocalDateTime;
public class LdtWithYearBirthday {
public static void main(String[] args) {
LocalDateTime born = LocalDateTime.of(1990, 7, 4, 0, 0);
LocalDateTime now = LocalDateTime.of(2026, 11, 19, 9, 0);
LocalDateTime thisYear = born.withYear(now.getYear());
System.out.println("birthday this year: " + thisYear);
System.out.println("already passed: " + thisYear.isBefore(now));
System.out.println("next birthday: " + born.withYear(now.getYear() + 1));
}
} birthday this year: 2026-07-04T00:00
already passed: true
next birthday: 2027-07-04T00:00 How It Works The birth date. 4 July 1990 at midnight. getYear() reads the current year, 2026.Move it to this year. withYear(2026) gives 4 July 2026.Compare with now. 4 July is before 19 November, so isBefore prints true.Use next year instead. Because it has already passed, the next birthday is in 2027.Frequently Asked Questions How do I change only the year of a LocalDateTime in Java? Call dt.withYear(value). It returns a new LocalDateTime with that part replaced and everything else copied.
Does withYear() change the original LocalDateTime? No. LocalDateTime is immutable, so withYear() returns a new object and leaves the original as it was.
What happens if the value passed to withYear() is not valid? A year outside the range -999999999 to 999999999 throws a DateTimeException. Any year inside the range is accepted, even one in the far past.
How do I set the year of a LocalDateTime in Java? Use dt.withYear(2030). It returns a new LocalDateTime with that year.
What is the difference between withYear() and plusYears()? withYear sets the year to a fixed number. plusYears adds a number to the current year.
Back to the method index See all 62 LocalDateTime method guides grouped by what they do.
Method index → 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.
14 people found this page helpful
Helpful Share Copy link Suggestion