Java LocalDateTime withYear() Method

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

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.

Java
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
CallWhat 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
// java.time.LocalDateTime
public LocalDateTime withYear(int year)

Parameters

ParameterDescription
year (int)The new year, from -999999999 to 999999999.

Return Value

Return ValueDescription
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:

MethodHow it differs
with(field, value)The general method: you choose the field as an argument.
plusYears(n)Adds years to the current year instead of setting one.
withMonth(m)Sets the month and keeps the year.

Java Examples

Example 1 — Move a Date to Another Year

Take 15 March 2026 at 10:30 and place it in the years 2030 and 1999.

Java
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));
    }
}

How It Works

  1. The start. The year is 2026, and the date-time is 15 March at 10:30.
  2. Set the year to 2030. withYear(2030) keeps the 15 March and the 10:30.
  3. 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.

Java
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));
    }
}

How It Works

  1. 29 February 2024. This date exists only in leap years.
  2. 2025 is not a leap year. There is no 29 February, so Java picks 28 February 2025.
  3. 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.

Java
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));
    }
}

How It Works

  1. The birth date. 4 July 1990 at midnight. getYear() reads the current year, 2026.
  2. Move it to this year. withYear(2026) gives 4 July 2026.
  3. Compare with now. 4 July is before 19 November, so isBefore prints true.
  4. Use next year instead. Because it has already passed, the next birthday is in 2027.

Frequently Asked Questions

Call dt.withYear(value). It returns a new LocalDateTime with that part replaced and everything else copied.
No. LocalDateTime is immutable, so withYear() returns a new object and leaves the original as it was.
A year outside the range -999999999 to 999999999 throws a DateTimeException. Any year inside the range is accepted, even one in the far past.
Use dt.withYear(2030). It returns a new LocalDateTime with that year.
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

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.

14 people found this page helpful