LAST UPDATED: JUNE 15, 2020
Java LocalDate getDayOfYear() Method With Examples
This method is used to get day of a year. It returns an integer value lies between 1 to 365 or 366 in a leap year. For example, a date 2020/01/05 will return 5 as a day of the year.
This method does not take any argument and returns an integer value. The syntax of the method is given below.
Syntax
public int getDayOfYear()
Parameters:
It does not take any parameter.
Returns:
It returns an integer value.
Time for an Example: Get day of Year
Let's take an example to get a day of year using the getDayOfYear()
method. In this example, we have a date of 25, February that will result in 56 days. See the below example.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2020, 2, 25);
System.out.println(localDate);
int year = localDate.getDayOfYear();
System.out.println("day of year : "+year);
}
}
2020-02-25
day of year : 56
Time for another Example:
If we want to know how many days have passed including today this year, we can use this method with current datetime that will result in the total day of the year. See the below example.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
int year = localDate.getDayOfYear();
System.out.println("day of year : "+year);
}
}
2020-06-03
day of year : 154
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.