Home »
Java »
Java Reference »
Java LocalDate Class
Java LocalDate Class | withDayOfYear() Method with Example
LocalDate Class withDayOfYear() method: Here, we are going to learn about the withDayOfYear() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on June 02, 2020
LocalDate Class withDayOfYear() method
- withDayOfYear() method is available in java.time package.
- withDayOfYear() method is used to update this object with the given day-of-year (dd_of_yyyy) in this LocalDate.
- withDayOfYear() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- withDayOfYear() method may throw an exception at the time of updating this object with the given day of the year.
DateTimeException: This exception may throw when the given parameter holds invalid value.
Syntax:
public LocalDate withDayOfYear(int dd_of_yyyy);
Parameter(s):
- int dd_of_yyyy – represents the day-of-year by which to update this object and the number of days in a year starts from 1 and ends at 366.
Return value:
The return type of this method is LocalDate, it returns the LocalDate that holds the value to update the day-of-year of this object with the given day-of-year.
Example:
// Java program to demonstrate the example
// of withDayOfYear(int dd_of_yyyy)
// method of LocalDate
import java.time.*;
public class WithDayOfYearOfLocalDate {
public static void main(String args[]) {
int day_of_year = 50;
// Instantiates two LocalDate
LocalDate l_da1 = LocalDate.parse("2007-04-04");
LocalDate l_da2 = LocalDate.of(2008, Month.FEBRUARY, 06);
// Display l_da1,l_da2
System.out.println("LocalDate l_da1,l_da2 : ");
System.out.println("l_da1: " + l_da1);
System.out.println("l_da2: " + l_da2);
System.out.println("day_of_year to update: " + day_of_year);
System.out.println();
// Here, this method updates day-of-year
// with the given value in this LocalDate
// l_da1
LocalDate l_date = l_da1.withDayOfYear(day_of_year);
// Display l_date
System.out.println("l_da1.withDayOfYear(day_of_year): " + l_date);
// Here, this method updates day-of-year
// with the given value in this
// LocalDate l_da2
l_date = l_da2.withDayOfYear(day_of_year);
// Display l_date
System.out.println("l_da2.withDayOfYear(day_of_year): " + l_date);
}
}
Output
LocalDate l_da1,l_da2 :
l_da1: 2007-04-04
l_da2: 2008-02-06
day_of_year to update: 50
l_da1.withDayOfYear(day_of_year): 2007-02-19
l_da2.withDayOfYear(day_of_year): 2008-02-19