Year atDay() method in Java

The atDay() method of Year class in Java combines the current year with a day-of-year passed as parameter to it to create a LocalDate.

Syntax:

public LocalDate atDay(int dayOfYear)

Parameter: This method accepts a single parameter dayOfYear. It is the day-of-year to use. It can take values from 1 to 365-366.



Return Value: It returns a Local Date formed by the current year and a date of year passed as parameter to the function.

Exception: This method throws a DateTimeException, if the day of year passed in the parameter is not valid, that is zero or less, 366 or greater or equal to 366 and the current year is not a leap year.

Below programs illustrate the atDay() method of Year in Java:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Programt to illustrate the atDay() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
  
        // Creates a Year object
        Year thisYear = Year.of(2017);
  
        // Creates a local date with this
        // Year object and day passed to it
        LocalDate date = thisYear.atDay(31);
  
        System.out.println(date);
    }
}

chevron_right


Output:

2017-01-31

Program 2: To illustrate Exception.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Programt to illustrate the atDay() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
  
        // Creates a Year object
        Year thisYear = Year.of(2017);
  
        // Creates a local date with this
        // Year object and day passed to it
        try {
            LocalDate date = thisYear.atDay(367);
            System.out.println(date);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

java.time.DateTimeException: Invalid value for DayOfYear (valid values 1 - 365/366): 367

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#atDay-int-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.