Java LocalDate Class | ofYearDay() Method with Example

LocalDate Class ofYearDay() method: Here, we are going to learn about the ofYearDay() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on May 31, 2020

LocalDate Class ofYearDay() method

  • ofYearDay() method is available in java.time package.
  • ofYearDay() method is used to create an instance of LocalDate object that holds the value from the year and day-of-year.
  • ofYearDay() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • ofYearDay() method may throw an exception at the time of representing the day of the year.
    DateTimeException: This exception may throw when the calculated result value exceeds the limit.

Syntax:

    public LocalDate ofYearDay(int yyyy, int ddOfyyyy);

Parameter(s):

  • int yyyy – represents the year and starts from MIN_YEAR to MAX_YEAR.
  • int ddOfyyyy – represents the days in a year and the number of days in a year from 1 to 365 (for non-leap) 366 (for leap year).

Return value:

The return type of this method is LocalDate, it returns the LocalDate that holds the value represented from a year and day-of-year.

Example:

// Java program to demonstrate the example 
// of ofYearDay(int yyyy, int ddOfyyyy)
// method of LocalDate

import java.time.*;

public class OfYearDayOfLocalDate {
    public static void main(String args[]) {
        int year = 2008;
        int day_of_year = 50;

        // Here, this method creates an instance
        // of LocalDate by using the given year
        // and day_of_year i.e. 2008-02-19
        // [year = 2008, day_of_year = 50 
        // (Jan = 31 days + Feb = 19 days )
        // {2008, Feb, 19} so 2008-02-19]
        LocalDate of_year_day = LocalDate.ofYearDay(year, day_of_year);

        // Display of_year_day
        System.out.println("LocalDate.ofYearDay(year,day_of_year): " + of_year_day);
    }
}

Output

LocalDate.ofYearDay(year,day_of_year): 2008-02-19


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.