The withDayOfYear() method of LocalDate class in Java returns a copy of this LocalDate with the day-of-year altered.
public LocalDate withDayOfYear(int dayOfYear)
Parameter: This method accepts a mandatory parameter dayOfYear which specifies the day-of-year to set in the result, from 1 to 365-366.
Returns: The function returns a LocalDate based on this date with the requested day, not null.
Exceptions: The function throws a DateTimeException when the day-of-year value is invalid.
Below programs illustrate the LocalDate.withDayOfYear() method:
The date with day of year is: 2018-01-01
java.time.DateTimeException: Invalid value for DayOfYear (valid values 1 - 365/366): 370
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.