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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/classes_objects -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/class_methods -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/date -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/modifiers -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/oop -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/packages_api -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/identifier -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/arrays -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/comments -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/data_types -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/operators -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/output -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/strings -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/variables -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/string_methods -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} java/system_methods -.-> lab-117790{{"`Java LocalDate GetDayOfYear Method`"}} end

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