Open In App

LocalDate ofYearDay() method in Java with Examples

The ofYearDay(int year, int dayOfYear) method of LocalDate class in Java is used to obtain an instance of LocalDate from an input year and day-of-year. Here no value of the month is passed. The passed year forms the year of the instance and the day and month are calculated on the basis of dayOfYear. Here 1st January forms the starting day. So, if dayOfYear is passed as 32, it means that the first 31 days form the month of January and the 32nd day is 1st of February.

Syntax:



public static LocalDate
          ofYearDay(int year, 
                    int dayOfYear)

Parameters: This method accepts two parameters:

Return Value: This method returns the localdate.



Exceptions: This method throws DateTimeException if the value of any field is out of range or the day-of-year is invalid.

Below programs illustrate the ofYearDay(int year, int dayOfYear) method in Java:

Program 1:




// Java program to demonstrate
// LocalDate.ofYearDay(int year,
// int dayOfYear) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create LocalDate object
        LocalDate localdate
            = LocalDate.ofYearDay(
                2020, 134);
  
        // print full date
        System.out.println("Date: "
                           + localdate);
    }
}

Output:
Date: 2020-05-13

Program 2:




// Java program to demonstrate
// LocalDate.ofYearDay(int year,
int dayOfYear) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create LocalDate object
        LocalDate localdate
            = LocalDate.ofYearDay(
                2020, 134);
  
        // print month
        System.out.println(
            "Month: "
            + localdate.getMonth());
    }
}

Output:
Month: MAY

References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#ofYearDay(int, int)


Article Tags :