Use getDayOfYear() Method to Get the Day of the Year
Create a LocalDate
instance using the of()
method and specify a date. Call the getDayOfYear()
method on the instance to get the day of the year.
public class LocalDateGetDayOfYearExample {
public static void main(String[] args){
// Create a LocalDate instance
LocalDate localDate = LocalDate.of(2021, 6, 10);
// Get the day of the year using getDayOfYear() method
int dayOfYear = localDate.getDayOfYear();
// Print the day of the year
System.out.println("Day Of Year: " + dayOfYear);
}
}
In the above code, a LocalDate
instance is created with date June 10, 2021 using the of()
method. The getDayOfYear()
method is called on the localDate
instance to get the day of the year. Finally, the day of the year is printed using the println()
method.
Compile and run the code using the following commands:
javac LocalDateGetDayOfYearExample.java
java LocalDateGetDayOfYearExample
You should be able to see the following output:
Day Of Year: 161