Java LocalDate GetDayOfYear Method

JavaJavaBeginner
Practice Now

Introduction

The getDayOfYear() method returns the day of the year for a given date. This method has no parameters and returns an integer value. To use the getDayOfYear() method, you need to create an instance of the LocalDate class and call the method on this instance.

Import Required Class

Import the necessary class for using LocalDate and DateTimeFormatter classes:

import java.time.LocalDate;

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

Get Current Day of the Year

To get the current day of the year, create a LocalDate instance with now() method and call getDayOfYear() method on the instance.

public class LocalDateGetDayOfYearExample {
    public static void main(String[] args){
        // Create a LocalDate instance
        LocalDate localDate = LocalDate.now();

        // 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 current date using the now() 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 current day of the year as output.

Use DateTimeFormatter to get the Day of the Year in String Format

You can use DateTimeFormatter class to format the date into a string of our choice.

import java.time.format.DateTimeFormatter;

public class LocalDateGetDayOfYearExample {
    public static void main(String[] args){
        // Create a LocalDate instance
        LocalDate localDate = LocalDate.now();

        // Create an instance of DateTimeFormatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // Format the LocalDate into String
        String formatDate = localDate.format(formatter);

        // 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 for " + formatDate + " is " + dayOfYear);
    }
}

In the above code, a DateTimeFormatter instance is created to format the date into the format "yyyy-MM-dd". Then, the current date is formatted into string with this format. The getDayOfYear() method is called on the localDate instance to get the day of the year. Finally, the day of the year and the formatted date string are 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 current day of the year along with the formatted date string as output.

Summary

In this lab, you have learned how to use the getDayOfYear() method in Java's LocalDate class. You have also learned how to get the current day of the year and format the date into a string of our choice.

Other Java Tutorials you may like